Add Entry constructor for new Drive REST API

pull/40/head
Vitaliy Filippov 2015-05-16 19:38:43 +03:00
parent 2d8da5cab3
commit b0255d9699
4 changed files with 67 additions and 8 deletions

View File

@ -26,6 +26,9 @@
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
#include "json/Val.hh"
#include "drive2/CommonUri.hh"
#include <algorithm>
#include <iterator>
@ -42,7 +45,7 @@ Entry::Entry( ) :
{
}
/// construct an entry for remote
/// construct an entry for remote - Doclist API v3
Entry::Entry( const xml::Node& n ) :
m_change_stamp( -1 ),
m_is_removed( false )
@ -50,6 +53,14 @@ Entry::Entry( const xml::Node& n ) :
Update( n ) ;
}
/// construct an entry for remote, from "file" JSON object - Drive REST API
Entry::Entry( const Val& item ) :
m_change_stamp( -1 ),
m_is_removed( false )
{
Update( item ) ;
}
void Entry::Update( const xml::Node& n )
{
m_title = n["title"] ;
@ -85,6 +96,45 @@ void Entry::Update( const xml::Node& n )
m_is_removed = !n["gd:deleted"].empty() || !n["docs:removed"].empty() ;
}
void Entry::Update( const Val& item )
{
bool is_chg = item["kind"].Str() == "drive#change";
// changestamp only appears in change feed entries
m_change_stamp = is_chg ? item["id"].Int() : -1 ;
m_is_removed = is_chg && item["deleted"].Bool() ;
const Val& file = is_chg && !m_is_removed ? item["file"] : item;
if ( m_is_removed )
{
m_resource_id = item["fileId"];
}
else
{
m_title = file["title"] ;
m_etag = file["etag"] ;
m_filename = file["originalFilename"] ;
m_content_src = file["downloadUrl"] ;
m_self_href = file["selfLink"] ;
m_mtime = DateTime( file["modificationDate"] ) ;
m_resource_id = file["id"]; // file#id ?
m_md5 = file["md5Checksum"] ;
m_is_dir = file["mimeType"].Str() == v2::mime_types::folder ;
m_is_editable = file["editable"].Bool() ;
m_parent_hrefs.clear( ) ;
Val::Array parents = file["parents"].AsArray() ;
for ( Val::Array::iterator i = parents.begin() ; i != parents.end() ; ++i )
m_parent_hrefs.push_back( (*i)["parentLink"] ) ; // maybe .id?
// convert to lower case for easy comparison
std::transform( m_md5.begin(), m_md5.end(), m_md5.begin(), tolower ) ;
}
}
const std::vector<std::string>& Entry::ParentHrefs() const
{
return m_parent_hrefs ;

View File

@ -28,6 +28,8 @@
namespace gr {
class Val ;
namespace xml
{
class Node ;
@ -46,6 +48,7 @@ class Entry
public :
Entry( ) ;
explicit Entry( const xml::Node& n ) ;
explicit Entry( const Val& item ) ;
std::string Title() const ;
std::string Filename() const ;
@ -69,9 +72,10 @@ public :
const std::vector<std::string>& ParentHrefs() const ;
void Update( const xml::Node& entry ) ;
private :
void Update( const xml::Node& entry ) ;
void Update( const Val& item ) ;
std::string m_title ;
std::string m_filename ;
bool m_is_dir ;

View File

@ -107,17 +107,22 @@ std::string Val::Str() const
return As<std::string>() ;
}
int Val::Int() const
Val::operator std::string() const
{
return As<std::string>() ;
}
int Val::Int() const
{
return static_cast<int>(As<long long>()) ;
}
double Val::Double() const
double Val::Double() const
{
return As<double>() ;
}
bool Val::Bool() const
bool Val::Bool() const
{
return As<bool>() ;
}

View File

@ -81,6 +81,8 @@ public :
return Assign(t) ;
}
operator std::string() const ;
template <typename T>
const T& As() const ;
@ -111,8 +113,6 @@ public :
// shortcuts for array (and array of objects)
void Add( const Val& json ) ;
Val FindInArray( const std::string& key, const std::string& value ) const ;
bool FindInArray( const std::string& key, const std::string& value, Val& result ) const ;
std::vector<Val> Select( const std::string& key ) const ;