Merge lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run into lp:unity-scope-vimeo

Proposed by Marcus Tomlinson
Status: Merged
Merged at revision: 83
Proposed branch: lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run
Merge into: lp:unity-scope-vimeo
Diff against target: 475 lines (+126/-146)
7 files modified
CMakeLists.txt (+1/-1)
include/vimeo/api/client.h (+4/-2)
include/vimeo/scope/query.h (+2/-1)
include/vimeo/scope/scope.h (+0/-18)
src/vimeo/api/client.cpp (+112/-15)
src/vimeo/scope/query.cpp (+5/-4)
src/vimeo/scope/scope.cpp (+2/-105)
To merge this branch: bzr merge lp:~marcustomlinson/unity-scope-vimeo/request_token_from_run
Reviewer Review Type Date Requested Status
Unity API Team Pending
Review via email: mp+243650@code.launchpad.net

Commit message

Create one OnlineAccountClient instance and pass it into Query and Preview objects. This instance is then used by an internal Client object to keep its config up to date (by refreshing the service statuses each time the token is needed).

This way the config can be completely encapsulated within Client, and Client can be encapsulated within Query and Preview.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2014-11-18 17:34:05 +0000
3+++ CMakeLists.txt 2014-12-04 12:41:10 +0000
4@@ -49,7 +49,7 @@
5 ${SCOPE_INCLUDE_DIRS}
6 )
7
8-set(SCOPE_VERSION "1.0.1-${BZR_REVNO}")
9+set(SCOPE_VERSION "1.0.2-${BZR_REVNO}")
10 set(CMAKE_INSTALL_PREFIX /)
11 set(SCOPE_NAME "com.ubuntu.scopes.vimeo")
12 set(SCOPE_INSTALL_NAME "${SCOPE_NAME}_vimeo")
13
14=== modified file 'include/vimeo/api/client.h'
15--- include/vimeo/api/client.h 2014-11-17 12:44:51 +0000
16+++ include/vimeo/api/client.h 2014-12-04 12:41:10 +0000
17@@ -23,6 +23,8 @@
18 #include <vimeo/api/config.h>
19 #include <vimeo/api/video.h>
20
21+#include <unity/scopes/OnlineAccountClient.h>
22+
23 #include <atomic>
24 #include <deque>
25 #include <future>
26@@ -44,7 +46,7 @@
27
28 typedef std::deque<Video::Ptr> VideoList;
29
30- Client(Config::Ptr config);
31+ Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client);
32
33 virtual ~Client() = default;
34
35@@ -58,7 +60,7 @@
36
37 virtual void cancel();
38
39- virtual Config::Ptr config();
40+ virtual bool authenticated();
41
42 protected:
43 class Priv;
44
45=== modified file 'include/vimeo/scope/query.h'
46--- include/vimeo/scope/query.h 2014-06-25 14:41:07 +0000
47+++ include/vimeo/scope/query.h 2014-12-04 12:41:10 +0000
48@@ -30,7 +30,8 @@
49 class Query: public unity::scopes::SearchQueryBase {
50 public:
51 Query(const unity::scopes::CannedQuery &query,
52- const unity::scopes::SearchMetadata &metadata, vimeo::api::Config::Ptr config);
53+ const unity::scopes::SearchMetadata &metadata,
54+ std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client);
55
56 ~Query() = default;
57
58
59=== modified file 'include/vimeo/scope/scope.h'
60--- include/vimeo/scope/scope.h 2014-11-03 10:04:16 +0000
61+++ include/vimeo/scope/scope.h 2014-12-04 12:41:10 +0000
62@@ -19,8 +19,6 @@
63 #ifndef VIMEO_SCOPE_SCOPE_H_
64 #define VIMEO_SCOPE_SCOPE_H_
65
66-#include <vimeo/api/config.h>
67-
68 #include <unity/scopes/ScopeBase.h>
69 #include <unity/scopes/OnlineAccountClient.h>
70 #include <unity/scopes/QueryBase.h>
71@@ -28,8 +26,6 @@
72 #include <unity/scopes/QueryBase.h>
73 #include <unity/scopes/PreviewQueryBase.h>
74
75-#include <condition_variable>
76-
77 namespace vimeo {
78
79 namespace scope {
80@@ -48,20 +44,6 @@
81 unity::scopes::SearchMetadata const&) override;
82
83 protected:
84- void anonymous_login();
85-
86- void service_update(unity::scopes::OnlineAccountClient::ServiceStatus const& status);
87-
88- void update_config();
89-
90- void init_config();
91-
92- vimeo::api::Config::Ptr config_;
93-
94- std::mutex config_mutex_;
95-
96- std::condition_variable config_cond_;
97-
98 std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_;
99 };
100
101
102=== modified file 'src/vimeo/api/client.cpp'
103--- src/vimeo/api/client.cpp 2014-11-17 12:54:17 +0000
104+++ src/vimeo/api/client.cpp 2014-12-04 12:41:10 +0000
105@@ -17,7 +17,10 @@
106 */
107
108 #include <vimeo/api/client.h>
109+#include <vimeo/api/login.h>
110
111+#include <boost/filesystem/operations.hpp>
112+#include <boost/filesystem/path.hpp>
113 #include <boost/iostreams/filtering_stream.hpp>
114 #include <boost/iostreams/filter/gzip.hpp>
115 #include <core/net/error.h>
116@@ -26,7 +29,10 @@
117 #include <core/net/http/response.h>
118 #include <json/json.h>
119
120+#include <fstream>
121+
122 namespace http = core::net::http;
123+namespace fs = boost::filesystem;
124 namespace io = boost::iostreams;
125 namespace json = Json;
126 namespace net = core::net;
127@@ -34,6 +40,9 @@
128 using namespace vimeo::api;
129 using namespace std;
130
131+static const char* CLIENT_ID = "b6758ff9f929cdb9f45a8477732bdbc4c6a89c7e";
132+static const char* CLIENT_SECRET = "a3222f38f799b3b528e29418fe062c02c677a249";
133+
134 namespace {
135
136 template<typename T>
137@@ -50,9 +59,9 @@
138
139 class Client::Priv {
140 public:
141- Priv(Config::Ptr config) :
142- client_(http::make_client()), worker_ { [this]() {client_->run();} }, config_(
143- config), cancelled_(false) {
144+ Priv(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) :
145+ client_(http::make_client()), worker_ { [this]() {client_->run();} },
146+ oa_client_(oa_client), cancelled_(false) {
147 }
148
149 ~Priv() {
150@@ -66,31 +75,36 @@
151
152 std::thread worker_;
153
154- Config::Ptr config_;
155+ Config config_;
156+ std::mutex config_mutex_;
157+
158+ std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_;
159
160 std::atomic<bool> cancelled_;
161
162 void get(const net::Uri::Path &path,
163 const net::Uri::QueryParameters &parameters,
164 http::Request::Handler &handler) {
165+ std::lock_guard<std::mutex> lock(config_mutex_);
166+ update_config();
167
168 http::Request::Configuration configuration;
169 net::Uri::QueryParameters complete_parameters(parameters);
170
171- net::Uri uri = net::make_uri(config_->apiroot, path,
172+ net::Uri uri = net::make_uri(config_.apiroot, path,
173 complete_parameters);
174 configuration.uri = client_->uri_to_string(uri);
175- if (!config_->access_token.empty()) {
176+ if (!config_.access_token.empty()) {
177 configuration.header.add("Authorization",
178- "bearer " + config_->access_token);
179- } else if (!config_->client_id.empty() && !config_->client_secret.empty()) {
180+ "bearer " + config_.access_token);
181+ } else if (!config_.client_id.empty() && !config_.client_secret.empty()) {
182 string auth = "basic "
183 + client_->base64_encode(
184- config_->client_id + ":" + config_->client_secret);
185+ config_.client_id + ":" + config_.client_secret);
186 configuration.header.add("Authorization", auth);
187 }
188- configuration.header.add("Accept", config_->accept);
189- configuration.header.add("User-Agent", config_->user_agent + " (gzip)");
190+ configuration.header.add("Accept", config_.accept);
191+ configuration.header.add("User-Agent", config_.user_agent + " (gzip)");
192 configuration.header.add("Accept-Encoding", "gzip");
193
194 auto request = client_->head(configuration);
195@@ -151,10 +165,93 @@
196
197 return prom->get_future();
198 }
199+
200+ bool authenticated() {
201+ std::lock_guard<std::mutex> lock(config_mutex_);
202+ update_config();
203+ return config_.authenticated;
204+ }
205+
206+ void anonymous_login() {
207+ fs::path saved_token_dir = fs::path(getenv("HOME"))
208+ / ".local" / "share" / "unity-scopes" / "leaf-net" / SCOPE_NAME;
209+ fs::path saved_token_path = saved_token_dir
210+ / "anonymous_auth_token";
211+
212+ bool save_auth_token = getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") == nullptr;
213+
214+ config_.client_id = CLIENT_ID;
215+ config_.client_secret = CLIENT_SECRET;
216+
217+ if (fs::exists(saved_token_path) && save_auth_token) {
218+ ifstream in(saved_token_path.native(), ios::in | ios::binary);
219+ if (in) {
220+ ostringstream contents;
221+ contents << in.rdbuf();
222+ in.close();
223+ config_.access_token = contents.str();
224+ }
225+ cerr << " re-using saved auth_token" << endl;
226+ } else {
227+ config_.access_token = unauthenticated(CLIENT_ID, CLIENT_SECRET,
228+ config_.apiroot);
229+ if (save_auth_token) {
230+ fs::create_directories(saved_token_dir);
231+ ofstream out(saved_token_path.native(), ios::out | ios::binary);
232+ if (out) {
233+ out << config_.access_token;
234+ out.close();
235+ }
236+ }
237+ cerr << " new auth_token" << endl;
238+ }
239+ }
240+
241+ void update_config() {
242+ if (getenv("VIMEO_SCOPE_APIROOT")) {
243+ config_.apiroot = getenv("VIMEO_SCOPE_APIROOT");
244+ }
245+
246+ if (getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") != nullptr) {
247+ return;
248+ }
249+
250+ /// TODO: The code commented out below should be uncommented as soon as
251+ /// OnlineAccountClient::refresh_service_statuses() is fixed (Bug #1398813).
252+ /// For now we have to re-instantiate a new OnlineAccountClient each time.
253+
254+ ///if (oa_client_ == nullptr) {
255+ oa_client_.reset(
256+ new unity::scopes::OnlineAccountClient(SCOPE_INSTALL_NAME,
257+ "sharing", SCOPE_ACCOUNTS_NAME));
258+ ///} else {
259+ /// oa_client_->refresh_service_statuses();
260+ ///}
261+
262+ for (auto const& status : oa_client_->get_service_statuses()) {
263+ if (status.service_authenticated) {
264+ config_.authenticated = true;
265+ config_.access_token = status.access_token;
266+ config_.client_id = status.client_id;
267+ config_.client_secret = status.client_secret;
268+ break;
269+ }
270+ }
271+
272+ if (!config_.authenticated) {
273+ config_.access_token = "";
274+ config_.client_id = "";
275+ config_.client_secret = "";
276+ std::cerr << "Vimeo scope is unauthenticated" << std::endl;
277+ anonymous_login();
278+ } else {
279+ std::cerr << "Vimeo scope is authenticated" << std::endl;
280+ }
281+ }
282 };
283
284-Client::Client(Config::Ptr config) :
285- p(new Priv(config)) {
286+Client::Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) :
287+ p(new Priv(oa_client)) {
288 }
289
290 future<Client::VideoList> Client::videos(const string &query) {
291@@ -190,7 +287,7 @@
292 p->cancelled_ = true;
293 }
294
295-Config::Ptr Client::config() {
296- return p->config_;
297+bool Client::authenticated() {
298+ return p->authenticated();
299 }
300
301
302=== modified file 'src/vimeo/scope/query.cpp'
303--- src/vimeo/scope/query.cpp 2014-11-17 12:44:51 +0000
304+++ src/vimeo/scope/query.cpp 2014-12-04 12:41:10 +0000
305@@ -88,8 +88,9 @@
306 }
307
308 Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata,
309- Config::Ptr config) :
310- sc::SearchQueryBase(query, metadata), client_(config) {
311+ std::shared_ptr<sc::OnlineAccountClient> oa_client) :
312+ sc::SearchQueryBase(query, metadata),
313+ client_(oa_client) {
314 }
315
316 void Query::cancelled() {
317@@ -132,7 +133,7 @@
318 "My Feed");
319 sc::Department::SPtr dummy_dept;
320
321- bool include_login_nag = !client_.config()->authenticated;
322+ bool include_login_nag = !client_.authenticated();
323
324 if (query_string.empty()) {
325 channels_future = client_.channels();
326@@ -147,7 +148,7 @@
327 videos_future = client_.channels_videos("staffpicks");
328 } else if (!query.department_id().empty()) {
329 videos_future = client_.channels_videos(query.department_id());
330- } else if (client_.config()->authenticated) {
331+ } else if (client_.authenticated()) {
332 videos_future = client_.feed();
333 } else {
334 videos_future = client_.channels_videos("staffpicks");
335
336=== modified file 'src/vimeo/scope/scope.cpp'
337--- src/vimeo/scope/scope.cpp 2014-11-07 18:54:12 +0000
338+++ src/vimeo/scope/scope.cpp 2014-12-04 12:41:10 +0000
339@@ -16,102 +16,16 @@
340 * Author: Pete Woods <pete.woods@canonical.com>
341 */
342
343-#include <vimeo/api/login.h>
344+#include <vimeo/scope/localisation.h>
345 #include <vimeo/scope/scope.h>
346 #include <vimeo/scope/query.h>
347 #include <vimeo/scope/preview.h>
348
349-#include <boost/filesystem/path.hpp>
350-#include <boost/filesystem/operations.hpp>
351-
352-#include <iostream>
353-#include <sstream>
354-#include <fstream>
355-
356 namespace sc = unity::scopes;
357 using namespace std;
358-using namespace boost;
359 using namespace vimeo::scope;
360 using namespace vimeo::api;
361
362-static const char* CLIENT_ID = "b6758ff9f929cdb9f45a8477732bdbc4c6a89c7e";
363-static const char* CLIENT_SECRET = "a3222f38f799b3b528e29418fe062c02c677a249";
364-
365-void Scope::anonymous_login() {
366- filesystem::path saved_token_dir = filesystem::path(getenv("HOME"))
367- / ".local" / "share" / "unity-scopes" / "leaf-net" / SCOPE_NAME;
368- filesystem::path saved_token_path = saved_token_dir
369- / "anonymous_auth_token";
370-
371- bool save_auth_token = getenv("VIMEO_SCOPE_IGNORE_ACCOUNTS") == nullptr;
372-
373- config_->client_id = CLIENT_ID;
374- config_->client_secret = CLIENT_SECRET;
375-
376- if (filesystem::exists(saved_token_path) && save_auth_token) {
377- ifstream in(saved_token_path.native(), ios::in | ios::binary);
378- if (in) {
379- ostringstream contents;
380- contents << in.rdbuf();
381- in.close();
382- config_->access_token = contents.str();
383- }
384- cerr << " re-using saved auth_token" << endl;
385- } else {
386- config_->access_token = unauthenticated(CLIENT_ID, CLIENT_SECRET,
387- config_->apiroot);
388- if (save_auth_token) {
389- filesystem::create_directories(saved_token_dir);
390- ofstream out(saved_token_path.native(), ios::out | ios::binary);
391- if (out) {
392- out << config_->access_token;
393- out.close();
394- }
395- }
396- cerr << " new auth_token" << endl;
397- }
398-}
399-
400-void Scope::service_update(sc::OnlineAccountClient::ServiceStatus const&)
401-{
402- update_config();
403-}
404-
405-void Scope::update_config()
406-{
407- std::lock_guard<std::mutex> lock(config_mutex_);
408- init_config();
409-
410- for (auto const& status : oa_client_->get_service_statuses())
411- {
412- if (status.service_authenticated)
413- {
414- config_->authenticated = true;
415- config_->access_token = status.access_token;
416- config_->client_id = status.client_id;
417- config_->client_secret = status.client_secret;
418- break;
419- }
420- }
421-
422- if (!config_->authenticated) {
423- cerr << "Vimeo scope is unauthenticated" << endl;
424- anonymous_login();
425- } else {
426- cerr << "Vimeo scope is authenticated" << endl;
427- }
428-
429- config_cond_.notify_all();
430-}
431-
432-void Scope::init_config()
433-{
434- config_ = make_shared<Config>();
435- if (getenv("VIMEO_SCOPE_APIROOT")) {
436- config_->apiroot = getenv("VIMEO_SCOPE_APIROOT");
437- }
438-}
439-
440 void Scope::start(string const&) {
441 setlocale(LC_ALL, "");
442 string translation_directory = ScopeBase::scope_directory()
443@@ -122,23 +36,6 @@
444 oa_client_.reset(
445 new sc::OnlineAccountClient(SCOPE_INSTALL_NAME,
446 "sharing", SCOPE_ACCOUNTS_NAME));
447- oa_client_->set_service_update_callback(
448- std::bind(&Scope::service_update, this, std::placeholders::_1));
449-
450- ///! TODO: We should only be waiting here if we know that there is at least one Google account enabled.
451- /// OnlineAccountClient needs to expose some functionality for us to determine that.
452-
453- // Allow 1 second for the callback to initialize config_
454- std::unique_lock<std::mutex> lock(config_mutex_);
455- config_cond_.wait_for(lock, std::chrono::seconds(1), [this] { return config_ != nullptr; });
456- }
457-
458- if (config_ == nullptr)
459- {
460- // If the callback was not invoked, default initialize config_
461- init_config();
462- cerr << "Vimeo scope is unauthenticated" << endl;
463- anonymous_login();
464 }
465 }
466
467@@ -147,7 +44,7 @@
468
469 sc::SearchQueryBase::UPtr Scope::search(const sc::CannedQuery &query,
470 const sc::SearchMetadata &metadata) {
471- return sc::SearchQueryBase::UPtr(new Query(query, metadata, config_));
472+ return sc::SearchQueryBase::UPtr(new Query(query, metadata, oa_client_));
473 }
474
475 sc::PreviewQueryBase::UPtr Scope::preview(sc::Result const& result,

Subscribers

People subscribed via source and target branches