Skip to content

Commit

Permalink
added AuthAgent, a wrapper around http agent
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Jul 19, 2012
1 parent 57c431d commit 28e8012
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 63 deletions.
5 changes: 4 additions & 1 deletion grive/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "drive/Drive.hh"

#include "http/CurlAgent.hh"
#include "protocol/AuthAgent.hh"
#include "protocol/OAuth2.hh"
#include "protocol/Json.hh"

Expand Down Expand Up @@ -172,7 +174,8 @@ int Main( int argc, char **argv )
}

OAuth2 token( refresh_token, client_id, client_secret ) ;
Drive drive( token, options ) ;
AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
Drive drive( &agent, options ) ;
drive.DetectChanges() ;

if ( vm.count( "dry-run" ) == 0 )
Expand Down
42 changes: 19 additions & 23 deletions libgrive/src/drive/Drive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#include "Entry.hh"
#include "Feed.hh"

#include "http/CurlAgent.hh"
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "protocol/OAuth2.hh"
// #include "protocol/OAuth2.hh"
#include "util/Destroy.hh"
#include "util/log/Log.hh"
#include "xml/Node.hh"
Expand All @@ -53,12 +53,11 @@ namespace
const std::string state_file = ".grive_state" ;
}

Drive::Drive( OAuth2& auth, const Json& options ) :
m_auth( auth ),
Drive::Drive( http::Agent *http, const Json& options ) :
m_http( http ),
m_state( state_file, options )
{
m_http_hdr.Add( "Authorization: Bearer " + m_auth.AccessToken() ) ;
m_http_hdr.Add( "GData-Version: 3.0" ) ;
assert( m_http != 0 ) ;
}

void Drive::FromRemote( const Entry& entry )
Expand Down Expand Up @@ -93,14 +92,14 @@ void Drive::SaveState()
m_state.Write( state_file ) ;
}

void Drive::SyncFolders( http::Agent *http )
void Drive::SyncFolders( )
{
assert( http != 0 ) ;
assert( m_http != 0 ) ;

Log( "Synchronizing folders", log::info ) ;

http::XmlResponse xml ;
http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, m_http_hdr ) ;
m_http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, http::Header() ) ;

Feed feed( xml.Response() ) ;
do
Expand All @@ -121,7 +120,7 @@ void Drive::SyncFolders( http::Agent *http )
m_state.FromRemote( e ) ;
}
}
} while ( feed.GetNext( http, m_http_hdr ) ) ;
} while ( feed.GetNext( m_http, http::Header() ) ) ;

m_state.ResolveEntry() ;
}
Expand All @@ -131,17 +130,15 @@ void Drive::DetectChanges()
Log( "Reading local directories", log::info ) ;
m_state.FromLocal( "." ) ;

http::CurlAgent http ;

long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;

SyncFolders( &http ) ;
SyncFolders( ) ;

Log( "Reading remote server file list", log::info ) ;
Feed feed ;
// feed.EnableLog( "/tmp/file", ".xml" ) ;
feed.Start( &http, m_http_hdr, feed_base + "?showfolders=true&showroot=true" ) ;
feed.Start( m_http, http::Header(), feed_base + "?showfolders=true&showroot=true" ) ;

m_resume_link = feed.Root()["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
Expand All @@ -152,15 +149,15 @@ void Drive::DetectChanges()
feed.begin(), feed.end(),
boost::bind( &Drive::FromRemote, this, _1 ) ) ;

} while ( feed.GetNext( &http, m_http_hdr ) ) ;
} while ( feed.GetNext( m_http, http::Header() ) ) ;

// pull the changes feed
if ( prev_stamp != -1 )
{
Log( "Detecting changes from last sync", log::info ) ;
Feed changes ;
// feed.EnableLog( "/tmp/changes", ".xml" ) ;
feed.Start( &http, m_http_hdr, ChangesFeed(prev_stamp+1) ) ;
feed.Start( m_http, http::Header(), ChangesFeed(prev_stamp+1) ) ;

std::for_each(
changes.begin(), changes.end(),
Expand All @@ -171,25 +168,24 @@ void Drive::DetectChanges()
void Drive::Update()
{
Log( "Synchronizing files", log::info ) ;
http::CurlAgent http ;
m_state.Sync( &http, m_http_hdr ) ;
m_state.Sync( m_http ) ;

UpdateChangeStamp( &http ) ;
UpdateChangeStamp( ) ;
}

void Drive::DryRun()
{
Log( "Synchronizing files (dry-run)", log::info ) ;
m_state.Sync( 0, m_http_hdr ) ;
m_state.Sync( 0 ) ;
}

void Drive::UpdateChangeStamp( http::Agent *http )
void Drive::UpdateChangeStamp( )
{
assert( http != 0 ) ;
assert( m_http != 0 ) ;

// get changed feed
http::XmlResponse xrsp ;
http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, m_http_hdr ) ;
m_http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, http::Header() ) ;

// we should go through the changes to see if it was really Grive to made that change
// maybe by recording the updated timestamp and compare it?
Expand Down
12 changes: 6 additions & 6 deletions libgrive/src/drive/Drive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ namespace http
}

class Entry ;
class OAuth2 ;
class Json ;

class Drive
{
public :
Drive( OAuth2& auth, const Json& options ) ;
Drive( http::Agent *http, const Json& options ) ;

void DetectChanges() ;
void Update() ;
Expand All @@ -51,15 +50,16 @@ public :
struct Error : virtual Exception {} ;

private :
void SyncFolders( http::Agent *http ) ;
void SyncFolders( ) ;
void file();
void FromRemote( const Entry& entry ) ;
void FromChange( const Entry& entry ) ;
void UpdateChangeStamp( http::Agent *http ) ;
void UpdateChangeStamp( ) ;

private :
OAuth2& m_auth ;
http::Header m_http_hdr ;
http::Agent *m_http ;
// OAuth2& m_auth ;
// http::Header m_http_hdr ;

std::string m_resume_link ;
State m_state ;
Expand Down
39 changes: 19 additions & 20 deletions libgrive/src/drive/Resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,20 +365,20 @@ Resource* Resource::FindChild( const std::string& name )
}

// try to change the state to "sync"
void Resource::Sync( http::Agent *http, const http::Header& auth, DateTime& sync_time )
void Resource::Sync( http::Agent *http, DateTime& sync_time )
{
assert( m_state != unknown ) ;
assert( !IsRoot() || m_state == sync ) ; // root folder is already synced

SyncSelf( http, auth, sync_time ) ;
SyncSelf( http, sync_time ) ;

// if myself is deleted, no need to do the childrens
if ( m_state != local_deleted && m_state != remote_deleted )
std::for_each( m_child.begin(), m_child.end(),
boost::bind( &Resource::Sync, _1, http, auth, boost::ref(sync_time) ) ) ;
boost::bind( &Resource::Sync, _1, http, boost::ref(sync_time) ) ) ;
}

void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime& sync_time )
void Resource::SyncSelf( http::Agent* http, DateTime& sync_time )
{
assert( !IsRoot() || m_state == sync ) ; // root is always sync
assert( IsRoot() || http == 0 || fs::is_directory( m_parent->Path() ) ) ;
Expand All @@ -392,19 +392,19 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
case local_new :
Log( "sync %1% doesn't exist in server, uploading", path, log::info ) ;

if ( http != 0 && Create( http, auth, sync_time ) )
if ( http != 0 && Create( http, sync_time ) )
m_state = sync ;
break ;

case local_deleted :
Log( "sync %1% deleted in local. deleting remote", path, log::info ) ;
if ( http != 0 )
DeleteRemote( http, auth ) ;
DeleteRemote( http ) ;
break ;

case local_changed :
Log( "sync %1% changed in local. uploading", path, log::info ) ;
if ( http != 0 && EditContent( http, auth, sync_time ) )
if ( http != 0 && EditContent( http, sync_time ) )
m_state = sync ;
break ;

Expand All @@ -415,7 +415,7 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
if ( IsFolder() )
fs::create_directories( path ) ;
else
Download( http, path, auth ) ;
Download( http, path ) ;

m_state = sync ;
}
Expand All @@ -426,7 +426,7 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
Log( "sync %1% changed in remote. downloading", path, log::info ) ;
if ( http != 0 )
{
Download( http, path, auth ) ;
Download( http, path ) ;
m_state = sync ;
}
break ;
Expand Down Expand Up @@ -474,14 +474,14 @@ void Resource::DeleteLocal()
}
}

void Resource::DeleteRemote( http::Agent *http, const http::Header& auth )
void Resource::DeleteRemote( http::Agent *http )
{
assert( http != 0 ) ;
http::StringResponse str ;

try
{
http::Header hdr( auth ) ;
http::Header hdr ;
hdr.Add( "If-Match: " + m_etag ) ;

// doesn't know why, but an update before deleting seems to work always
Expand All @@ -502,12 +502,12 @@ void Resource::DeleteRemote( http::Agent *http, const http::Header& auth )
}


void Resource::Download( http::Agent* http, const fs::path& file, const http::Header& auth ) const
void Resource::Download( http::Agent* http, const fs::path& file ) const
{
assert( http != 0 ) ;

http::Download dl( file.string(), http::Download::NoChecksum() ) ;
long r = http->Get( m_content, &dl, auth ) ;
long r = http->Get( m_content, &dl, http::Header() ) ;
if ( r <= 400 )
{
if ( m_mtime != DateTime() )
Expand All @@ -517,7 +517,7 @@ void Resource::Download( http::Agent* http, const fs::path& file, const http::He
}
}

bool Resource::EditContent( http::Agent* http, const http::Header& auth, DateTime& sync_time )
bool Resource::EditContent( http::Agent* http, DateTime& sync_time )
{
assert( http != 0 ) ;
assert( m_parent != 0 ) ;
Expand All @@ -530,10 +530,10 @@ bool Resource::EditContent( http::Agent* http, const http::Header& auth, DateTim
return false ;
}

return Upload( http, m_edit, auth, false, sync_time ) ;
return Upload( http, m_edit, false, sync_time ) ;
}

bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sync_time )
bool Resource::Create( http::Agent* http, DateTime& sync_time )
{
assert( http != 0 ) ;
assert( m_parent != 0 ) ;
Expand All @@ -551,7 +551,7 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
% xml::Escape(m_name)
).str() ;

http::Header hdr( auth ) ;
http::Header hdr ;
hdr.Add( "Content-Type: application/atom+xml" ) ;

http::XmlResponse xml ;
Expand All @@ -563,7 +563,7 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
}
else if ( !m_parent->m_create.empty() )
{
return Upload( http, m_parent->m_create + "?convert=false", auth, true, sync_time ) ;
return Upload( http, m_parent->m_create + "?convert=false", true, sync_time ) ;
}
else
{
Expand All @@ -575,7 +575,6 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
bool Resource::Upload(
http::Agent* http,
const std::string& link,
const http::Header& auth,
bool post,
DateTime& sync_time )
{
Expand All @@ -585,7 +584,7 @@ bool Resource::Upload(
std::ostringstream xcontent_len ;
xcontent_len << "X-Upload-Content-Length: " << file.Size() ;

http::Header hdr( auth ) ;
http::Header hdr ;
hdr.Add( "Content-Type: application/atom+xml" ) ;
hdr.Add( "X-Upload-Content-Type: application/octet-stream" ) ;
hdr.Add( xcontent_len.str() ) ;
Expand Down
15 changes: 7 additions & 8 deletions libgrive/src/drive/Resource.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace gr {
namespace http
{
class Agent ;
class Header ;
}

class Entry ;
Expand Down Expand Up @@ -77,7 +76,7 @@ public :
void FromRemote( const Entry& remote, const DateTime& last_sync ) ;
void FromLocal( const DateTime& last_sync ) ;

void Sync( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ;
void Sync( http::Agent* http, DateTime& sync_time ) ;

// children access
iterator begin() const ;
Expand Down Expand Up @@ -124,19 +123,19 @@ private :
private :
void SetState( State new_state ) ;

void Download( http::Agent* http, const fs::path& file, const http::Header& auth ) const ;
bool EditContent( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ;
bool Create( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ;
bool Upload( http::Agent* http, const std::string& link, const http::Header& auth, bool post, DateTime& sync_time ) ;
void Download( http::Agent* http, const fs::path& file ) const ;
bool EditContent( http::Agent* http, DateTime& sync_time ) ;
bool Create( http::Agent* http, DateTime& sync_time ) ;
bool Upload( http::Agent* http, const std::string& link, bool post, DateTime& sync_time ) ;

void FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) ;
void FromRemoteFile( const Entry& remote, const DateTime& last_sync ) ;

void DeleteLocal() ;
void DeleteRemote( http::Agent* http, const http::Header& auth ) ;
void DeleteRemote( http::Agent* http ) ;

void AssignIDs( const Entry& remote ) ;
void SyncSelf( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ;
void SyncSelf( http::Agent* http, DateTime& sync_time ) ;

private :
std::string m_name ;
Expand Down
Loading

0 comments on commit 28e8012

Please sign in to comment.