Merge lp:~unity-api-team/unity-scope-soundcloud/request_token_from_run into lp:~unity-api-team/unity-scope-soundcloud/c++

Proposed by Marcus Tomlinson
Status: Merged
Merged at revision: 39
Proposed branch: lp:~unity-api-team/unity-scope-soundcloud/request_token_from_run
Merge into: lp:~unity-api-team/unity-scope-soundcloud/c++
Diff against target: 360 lines (+87/-95)
8 files modified
CMakeLists.txt (+1/-1)
include/api/client.h (+6/-2)
include/api/config.h (+0/-2)
include/scope/query.h (+2/-2)
include/scope/scope.h (+0/-12)
src/api/client.cpp (+72/-13)
src/scope/query.cpp (+5/-4)
src/scope/scope.cpp (+1/-59)
To merge this branch: bzr merge lp:~unity-api-team/unity-scope-soundcloud/request_token_from_run
Reviewer Review Type Date Requested Status
Pete Woods Approve
Review via email: mp+243668@code.launchpad.net

Commit message

Invoke OA on each query

To post a comment you must log in.
47. By Marcus Tomlinson

Increment version number

Revision history for this message
Pete Woods (pete-woods) wrote :

Looks good to me

review: Approve

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:36:21 +0000
3+++ CMakeLists.txt 2014-12-04 15:01:59 +0000
4@@ -55,7 +55,7 @@
5 set(UBUNTU_PROJECT_TYPE "Scope" CACHE INTERNAL "Tells QtCreator this is a Scope project")
6
7 # Important project paths
8-set(SCOPE_VERSION "1.0.0-${BZR_REVNO}")
9+set(SCOPE_VERSION "1.0.1-${BZR_REVNO}")
10 set(CMAKE_INSTALL_PREFIX /)
11 set(SCOPE_INSTALL_DIR "/soundcloud")
12 set(GETTEXT_PACKAGE "unity-scope-soundcloud")
13
14=== modified file 'include/api/client.h'
15--- include/api/client.h 2014-11-17 11:50:50 +0000
16+++ include/api/client.h 2014-12-04 15:01:59 +0000
17@@ -22,6 +22,8 @@
18 #include <api/config.h>
19 #include <api/track.h>
20
21+#include <unity/scopes/OnlineAccountClient.h>
22+
23 #include <atomic>
24 #include <deque>
25 #include <future>
26@@ -50,7 +52,7 @@
27 */
28 class Client {
29 public:
30- Client(Config::Ptr config);
31+ Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client);
32
33 virtual ~Client() = default;
34
35@@ -65,7 +67,9 @@
36 */
37 virtual void cancel();
38
39- virtual Config::Ptr config();
40+ virtual std::string client_id();
41+
42+ virtual bool authenticated();
43
44 protected:
45 class Priv;
46
47=== modified file 'include/api/config.h'
48--- include/api/config.h 2014-11-10 11:54:32 +0000
49+++ include/api/config.h 2014-12-04 15:01:59 +0000
50@@ -36,8 +36,6 @@
51 std::string access_token { };
52
53 bool authenticated = false;
54-
55- std::string directory {};
56 };
57
58 }
59
60=== modified file 'include/scope/query.h'
61--- include/scope/query.h 2014-11-12 12:02:30 +0000
62+++ include/scope/query.h 2014-12-04 15:01:59 +0000
63@@ -36,8 +36,8 @@
64 class Query: public unity::scopes::SearchQueryBase {
65 public:
66 Query(const unity::scopes::CannedQuery &query,
67- const unity::scopes::SearchMetadata &metadata,
68- api::Config::Ptr config);
69+ const unity::scopes::SearchMetadata &metadata,
70+ std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client);
71
72 ~Query() = default;
73
74
75=== modified file 'include/scope/scope.h'
76--- include/scope/scope.h 2014-11-12 10:49:13 +0000
77+++ include/scope/scope.h 2014-12-04 15:01:59 +0000
78@@ -19,11 +19,6 @@
79 #ifndef SCOPE_SCOPE_H_
80 #define SCOPE_SCOPE_H_
81
82-#include <mutex>
83-#include <condition_variable>
84-
85-#include <api/config.h>
86-
87 #include <unity/scopes/ScopeBase.h>
88 #include <unity/scopes/OnlineAccountClient.h>
89 #include <unity/scopes/QueryBase.h>
90@@ -68,13 +63,6 @@
91
92 protected:
93 std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_;
94-
95- std::mutex config_mutex_;
96- std::condition_variable config_cond_;
97- api::Config::Ptr config_;
98-
99- void init_config();
100- void update_config();
101 };
102
103 }
104
105=== modified file 'src/api/client.cpp'
106--- src/api/client.cpp 2014-11-17 11:50:50 +0000
107+++ src/api/client.cpp 2014-12-04 15:01:59 +0000
108@@ -74,9 +74,9 @@
109
110 class Client::Priv {
111 public:
112- Priv(Config::Ptr config) :
113- client_(http::make_client()), worker_ { [this]() {client_->run();} }, config_(
114- config), cancelled_(false) {
115+ Priv(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) :
116+ client_(http::make_client()), worker_ { [this]() {client_->run();} },
117+ oa_client_(oa_client), cancelled_(false) {
118 }
119
120 ~Priv() {
121@@ -90,27 +90,32 @@
122
123 std::thread worker_;
124
125- Config::Ptr config_;
126+ Config config_;
127+ std::mutex config_mutex_;
128+
129+ std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client_;
130
131 std::atomic<bool> cancelled_;
132
133 void get(const net::Uri::Path &path,
134 const net::Uri::QueryParameters &parameters,
135 http::Request::Handler &handler) {
136+ std::lock_guard<std::mutex> lock(config_mutex_);
137+ update_config();
138
139 http::Request::Configuration configuration;
140 net::Uri::QueryParameters complete_parameters(parameters);
141- if (config_->authenticated) {
142+ if (config_.authenticated) {
143 complete_parameters.emplace_back(
144- "oauth_token", config_->access_token);
145+ "oauth_token", config_.access_token);
146 } else {
147- complete_parameters.emplace_back("client_id", config_->client_id);
148+ complete_parameters.emplace_back("client_id", config_.client_id);
149 }
150
151- net::Uri uri = net::make_uri(config_->apiroot, path,
152+ net::Uri uri = net::make_uri(config_.apiroot, path,
153 complete_parameters);
154 configuration.uri = client_->uri_to_string(uri);
155- configuration.header.add("User-Agent", config_->user_agent + " (gzip)");
156+ configuration.header.add("User-Agent", config_.user_agent + " (gzip)");
157 configuration.header.add("Accept-Encoding", "gzip");
158
159 auto request = client_->head(configuration);
160@@ -170,10 +175,60 @@
161
162 return prom->get_future();
163 }
164+
165+ std::string client_id() {
166+ std::lock_guard<std::mutex> lock(config_mutex_);
167+ update_config();
168+ return config_.client_id;
169+ }
170+
171+ bool authenticated() {
172+ std::lock_guard<std::mutex> lock(config_mutex_);
173+ update_config();
174+ return config_.authenticated;
175+ }
176+
177+ void update_config() {
178+ config_ = Config();
179+
180+ if (getenv("NETWORK_SCOPE_APIROOT")) {
181+ config_.apiroot = getenv("NETWORK_SCOPE_APIROOT");
182+ }
183+
184+ if (getenv("SOUNDCLOUD_SCOPE_IGNORE_ACCOUNTS") != nullptr) {
185+ return;
186+ }
187+
188+ /// TODO: The code commented out below should be uncommented as soon as
189+ /// OnlineAccountClient::refresh_service_statuses() is fixed (Bug #1398813).
190+ /// For now we have to re-instantiate a new OnlineAccountClient each time.
191+
192+ ///if (oa_client_ == nullptr) {
193+ oa_client_.reset(new unity::scopes::OnlineAccountClient(
194+ SCOPE_NAME, "sharing", SCOPE_ACCOUNTS_NAME));
195+ ///} else {
196+ /// oa_client_->refresh_service_statuses();
197+ ///}
198+
199+ for (auto const& status : oa_client_->get_service_statuses()) {
200+ if (status.service_authenticated) {
201+ config_.authenticated = true;
202+ config_.access_token = status.access_token;
203+ config_.client_id = status.client_id;
204+ break;
205+ }
206+ }
207+
208+ if (!config_.authenticated) {
209+ std::cerr << "SoundCloud scope is unauthenticated" << std::endl;
210+ } else {
211+ std::cerr << "SoundCloud scope is authenticated" << std::endl;
212+ }
213+ }
214 };
215
216-Client::Client(Config::Ptr config) :
217- p(new Priv(config)) {
218+Client::Client(std::shared_ptr<unity::scopes::OnlineAccountClient> oa_client) :
219+ p(new Priv(oa_client)) {
220 }
221
222 future<deque<Track>> Client::search_tracks(const std::deque<std::pair<SP, std::string>> &parameters) {
223@@ -226,7 +281,11 @@
224 p->cancelled_ = true;
225 }
226
227-Config::Ptr Client::config() {
228- return p->config_;
229+std::string Client::client_id() {
230+ return p->client_id();
231+}
232+
233+bool Client::authenticated() {
234+ return p->authenticated();
235 }
236
237
238=== modified file 'src/scope/query.cpp'
239--- src/scope/query.cpp 2014-11-17 11:50:50 +0000
240+++ src/scope/query.cpp 2014-12-04 15:01:59 +0000
241@@ -182,8 +182,9 @@
242 }
243
244 Query::Query(const sc::CannedQuery &query, const sc::SearchMetadata &metadata,
245- Config::Ptr config) :
246- sc::SearchQueryBase(query, metadata), client_(config) {
247+ std::shared_ptr<sc::OnlineAccountClient> oa_client) :
248+ sc::SearchQueryBase(query, metadata),
249+ client_(oa_client) {
250 }
251
252 void Query::cancelled() {
253@@ -203,7 +204,7 @@
254 future<deque<Track>> stream_future;
255 bool reading_stream = false;
256 if (query_string.empty() && query.department_id().empty()) {
257- if (client_.config()->authenticated) {
258+ if (client_.authenticated()) {
259 first_cat = reply->register_category(
260 "stream", _("Stream"), "",
261 sc::CategoryRenderer(SEARCH_CATEGORY_TEMPLATE));
262@@ -271,7 +272,7 @@
263
264 res["label"] = track.label_name();
265 res["streamable"] = track.streamable();
266- res["stream-url"] = track.stream_url() + "?client_id=" + client_.config()->client_id;
267+ res["stream-url"] = track.stream_url() + "?client_id=" + client_.client_id();
268 res["purchase-url"] = track.purchase_url();
269 res["video-url"] = track.video_url();
270 res["waveform"] = track.waveform();
271
272=== modified file 'src/scope/scope.cpp'
273--- src/scope/scope.cpp 2014-11-13 09:25:33 +0000
274+++ src/scope/scope.cpp 2014-12-04 15:01:59 +0000
275@@ -21,46 +21,11 @@
276 #include <scope/query.h>
277 #include <scope/scope.h>
278
279-#include <iostream>
280-#include <sstream>
281-#include <fstream>
282-
283 namespace sc = unity::scopes;
284 using namespace std;
285 using namespace api;
286 using namespace scope;
287
288-void Scope::init_config() {
289- config_ = make_shared<Config>();
290- config_->directory = scope_directory();
291- // Under test we set a different API root
292- char *apiroot = getenv("NETWORK_SCOPE_APIROOT");
293- if (apiroot) {
294- config_->apiroot = apiroot;
295- }
296-}
297-
298-void Scope::update_config() {
299- std::lock_guard<std::mutex> lock(config_mutex_);
300- init_config();
301-
302- for (auto const& status : oa_client_->get_service_statuses()) {
303- if (status.service_authenticated) {
304- config_->authenticated = true;
305- config_->access_token = status.access_token;
306- config_->client_id = status.client_id;
307- break;
308- }
309- }
310-
311- if (config_->authenticated) {
312- cerr << "SoundCloud scope is authenticated" << endl;
313- } else {
314- cerr << "SoundCloud scope is unauthenticated" << endl;
315- }
316- config_cond_.notify_all();
317-}
318-
319 void Scope::start(string const&) {
320 setlocale(LC_ALL, "");
321 string translation_directory = ScopeBase::scope_directory()
322@@ -70,24 +35,6 @@
323 if (getenv("SOUNDCLOUD_SCOPE_IGNORE_ACCOUNTS") == nullptr) {
324 oa_client_.reset(new sc::OnlineAccountClient(
325 SCOPE_NAME, "sharing", SCOPE_ACCOUNTS_NAME));
326- oa_client_->set_service_update_callback(
327- [this](sc::OnlineAccountClient::ServiceStatus const &) {
328- this->update_config();
329- });
330-
331- ///! TODO: We should only be waiting here if we know that
332- /// there is at least one SoundCloud account enabled.
333- /// OnlineAccountClient needs to expose some
334- /// functionality for us to determine that.
335-
336- // Allow 1 second for the callback to initialize config_
337- std::unique_lock<std::mutex> lock(config_mutex_);
338- config_cond_.wait_for(lock, std::chrono::seconds(1), [this] { return config_ != nullptr; });
339- }
340-
341- if (config_ == nullptr) {
342- init_config();
343- cerr << "SoundCloud scope is unauthenticated" << endl;
344 }
345 }
346
347@@ -96,12 +43,7 @@
348
349 sc::SearchQueryBase::UPtr Scope::search(const sc::CannedQuery &query,
350 const sc::SearchMetadata &metadata) {
351- shared_ptr<Config> config;
352- {
353- std::lock_guard<std::mutex> lock(config_mutex_);
354- config = config_;
355- }
356- return sc::SearchQueryBase::UPtr(new Query(query, metadata, config));
357+ return sc::SearchQueryBase::UPtr(new Query(query, metadata, oa_client_));
358 }
359
360 sc::PreviewQueryBase::UPtr Scope::preview(sc::Result const& result,

Subscribers

People subscribed via source and target branches

to all changes: