Permalink
Browse files

added AuthAgent, a wrapper around http agent

  • Loading branch information...
1 parent 57c431d commit 28e8012ca52359c73fefab4bbf4f81030cadafbb @nestal nestal committed Jul 19, 2012
View
@@ -21,6 +21,8 @@
#include "drive/Drive.hh"
+#include "http/CurlAgent.hh"
+#include "protocol/AuthAgent.hh"
#include "protocol/OAuth2.hh"
#include "protocol/Json.hh"
@@ -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 )
@@ -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"
@@ -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 )
@@ -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
@@ -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() ;
}
@@ -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"] ;
@@ -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(),
@@ -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?
@@ -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() ;
@@ -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 ;
@@ -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() ) ) ;
@@ -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 ;
@@ -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 ;
}
@@ -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 ;
@@ -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
@@ -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() )
@@ -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 ) ;
@@ -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 ) ;
@@ -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 ;
@@ -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
{
@@ -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 )
{
@@ -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() ) ;
@@ -32,7 +32,6 @@ namespace gr {
namespace http
{
class Agent ;
- class Header ;
}
class Entry ;
@@ -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 ;
@@ -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 ;
Oops, something went wrong.

0 comments on commit 28e8012

Please sign in to comment.