fetching the next link

pull/40/head
Matchman Green 2012-05-06 18:14:36 +08:00
parent b90e032cda
commit 1eeaede474
2 changed files with 43 additions and 21 deletions

View File

@ -50,22 +50,32 @@ Drive::Drive( OAuth2& auth ) :
m_http_hdr.push_back( "Authorization: Bearer " + m_auth.AccessToken() ) ;
m_http_hdr.push_back( "GData-Version: 3.0" ) ;
Json resp = Json::Parse( http::Get( root_url + "?alt=json&showfolders=true", m_http_hdr )) ;
ConstructDirTree( ) ;
Json resp = Json::Parse( http::Get( root_url + "?alt=json", m_http_hdr )) ;
Json resume_link ;
if ( resp["feed"]["link"].FindInArray( "rel", "http://schemas.google.com/g/2005#resumable-create-media", resume_link ) )
m_resume_link = resume_link["href"].As<std::string>() ;
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
ConstructDirTree( entries ) ;
for ( Json::Array::iterator i = entries.begin() ; i != entries.end() ; ++i )
bool has_next = false ;
do
{
if ( !Collection::IsCollection( *i ) )
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
for ( Json::Array::iterator i = entries.begin() ; i != entries.end() ; ++i )
{
UpdateFile( *i ) ;
if ( !Collection::IsCollection( *i ) )
{
UpdateFile( *i ) ;
}
}
}
Json next ;
has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ;
if ( has_next )
resp = Json::Parse( http::Get( next["href"].Str(), m_http_hdr ) ) ;
} while ( has_next ) ;
}
Drive::~Drive( )
@ -93,24 +103,36 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href )
return (it != m_coll.end() && it->Href() == href) ? it : m_coll.end() ;
}
void Drive::ConstructDirTree( const std::vector<Json>& entries )
void Drive::ConstructDirTree( )
{
Json resp = Json::Parse( http::Get( root_url + "/-/folder?alt=json", m_http_hdr )) ;
assert( m_coll.empty() ) ;
std::map<std::string, std::string> parent_href ;
// first, get all collections from the query result
for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i )
while ( true )
{
if ( Collection::IsCollection( *i ) )
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
// first, get all collections from the query result
for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i )
{
m_coll.push_back( Collection( *i ) ) ;
parent_href.insert(
std::make_pair(
m_coll.back().Href(),
Collection::ParentHref( *i ) ) ) ;
if ( Collection::IsCollection( *i ) )
{
m_coll.push_back( Collection( *i ) ) ;
parent_href.insert(
std::make_pair(
m_coll.back().Href(),
Collection::ParentHref( *i ) ) ) ;
}
}
assert( m_coll.size() == parent_href.size() ) ;
Json next ;
if ( !resp["feed"]["link"].FindInArray( "rel", "next", next ) )
break ;
resp = Json::Parse( http::Get( next["href"].Str(), m_http_hdr ) ) ;
}
assert( m_coll.size() == parent_href.size() ) ;
// second, build up linkage between parent and child
std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ;

View File

@ -42,7 +42,7 @@ public :
private :
void UpdateFile( const Json& entry ) ;
void ConstructDirTree( const std::vector<Json>& entries ) ;
void ConstructDirTree( ) ;
FolderListIterator FindFolder( const std::string& href ) ;