diff options
Diffstat (limited to 'src/client.cpp')
-rw-r--r-- | src/client.cpp | 104 |
1 files changed, 93 insertions, 11 deletions
diff --git a/src/client.cpp b/src/client.cpp index 51c288d..2c655e2 100644 --- a/src/client.cpp +++ b/src/client.cpp | |||
@@ -105,7 +105,12 @@ namespace twitter { | |||
105 | std::string response_data; | 105 | std::string response_data; |
106 | if (performGet(url, response_code, response_data) && (response_code == 200)) | 106 | if (performGet(url, response_code, response_data) && (response_code == 200)) |
107 | { | 107 | { |
108 | _current_user = user(response_data); | 108 | try { |
109 | _current_user = user(response_data); | ||
110 | } catch (std::invalid_argument e) | ||
111 | { | ||
112 | // Ignore | ||
113 | } | ||
109 | } | 114 | } |
110 | } | 115 | } |
111 | 116 | ||
@@ -145,8 +150,13 @@ namespace twitter { | |||
145 | 150 | ||
146 | if (response_code == 200) | 151 | if (response_code == 200) |
147 | { | 152 | { |
148 | result = tweet(response_data); | 153 | try { |
149 | return response::ok; | 154 | result = tweet(response_data); |
155 | return response::ok; | ||
156 | } catch (std::invalid_argument e) | ||
157 | { | ||
158 | return response::invalid_response; | ||
159 | } | ||
150 | } else { | 160 | } else { |
151 | return codeForError(response_code, response_data); | 161 | return codeForError(response_code, response_data); |
152 | } | 162 | } |
@@ -186,7 +196,14 @@ namespace twitter { | |||
186 | return codeForError(response_code, response_data); | 196 | return codeForError(response_code, response_data); |
187 | } | 197 | } |
188 | 198 | ||
189 | auto response_json = json::parse(response_data); | 199 | json response_json; |
200 | try { | ||
201 | response_json = json::parse(response_data); | ||
202 | } catch (std::invalid_argument e) | ||
203 | { | ||
204 | return response::invalid_response; | ||
205 | } | ||
206 | |||
190 | media_id = response_json["media_id"].get<long>(); | 207 | media_id = response_json["media_id"].get<long>(); |
191 | 208 | ||
192 | curl_httppost* append_form_post = nullptr; | 209 | curl_httppost* append_form_post = nullptr; |
@@ -226,7 +243,13 @@ namespace twitter { | |||
226 | return codeForError(response_code, response_data); | 243 | return codeForError(response_code, response_data); |
227 | } | 244 | } |
228 | 245 | ||
229 | response_json = json::parse(response_data); | 246 | try { |
247 | response_json = json::parse(response_data); | ||
248 | } catch (std::invalid_argument e) | ||
249 | { | ||
250 | return response::invalid_response; | ||
251 | } | ||
252 | |||
230 | if (response_json.find("processing_info") != response_json.end()) | 253 | if (response_json.find("processing_info") != response_json.end()) |
231 | { | 254 | { |
232 | std::stringstream datastr; | 255 | std::stringstream datastr; |
@@ -244,7 +267,13 @@ namespace twitter { | |||
244 | return codeForError(response_code, response_data); | 267 | return codeForError(response_code, response_data); |
245 | } | 268 | } |
246 | 269 | ||
247 | response_json = json::parse(response_data); | 270 | try { |
271 | response_json = json::parse(response_data); | ||
272 | } catch (std::invalid_argument e) | ||
273 | { | ||
274 | return response::invalid_response; | ||
275 | } | ||
276 | |||
248 | if (response_json["processing_info"]["state"] == "succeeded") | 277 | if (response_json["processing_info"]["state"] == "succeeded") |
249 | { | 278 | { |
250 | break; | 279 | break; |
@@ -316,9 +345,42 @@ namespace twitter { | |||
316 | return unfollow(toUnfollow.getID()); | 345 | return unfollow(toUnfollow.getID()); |
317 | } | 346 | } |
318 | 347 | ||
319 | const user& client::getUser() const | 348 | response client::getUser(user& result) |
349 | { | ||
350 | if (!_current_user) | ||
351 | { | ||
352 | std::string url = "https://api.twitter.com/1.1/account/verify_credentials.json"; | ||
353 | long response_code; | ||
354 | std::string response_data; | ||
355 | if (performGet(url, response_code, response_data) && (response_code == 200)) | ||
356 | { | ||
357 | try { | ||
358 | _current_user = user(response_data); | ||
359 | } catch (std::invalid_argument e) | ||
360 | { | ||
361 | return response::invalid_response; | ||
362 | } | ||
363 | } | ||
364 | } | ||
365 | |||
366 | result = _current_user; | ||
367 | return response::ok; | ||
368 | } | ||
369 | |||
370 | configuration client::getConfiguration() | ||
320 | { | 371 | { |
321 | return _current_user; | 372 | if (!_configuration || (difftime(time(NULL), _last_configuration_update) > 60*60*24)) |
373 | { | ||
374 | long response_code; | ||
375 | std::string response_data; | ||
376 | if (performGet("https://api.twitter.com/1.1/help/configuration.json", response_code, response_data)) | ||
377 | { | ||
378 | _configuration = configuration(response_data); | ||
379 | _last_configuration_update = time(NULL); | ||
380 | } | ||
381 | } | ||
382 | |||
383 | return _configuration; | ||
322 | } | 384 | } |
323 | 385 | ||
324 | response client::getFriends(std::set<user_id>& _ret) | 386 | response client::getFriends(std::set<user_id>& _ret) |
@@ -350,7 +412,14 @@ namespace twitter { | |||
350 | 412 | ||
351 | if (response_code == 200) | 413 | if (response_code == 200) |
352 | { | 414 | { |
353 | json rjs = json::parse(response_data); | 415 | json rjs; |
416 | try { | ||
417 | rjs = json::parse(response_data); | ||
418 | } catch (std::invalid_argument e) | ||
419 | { | ||
420 | return response::invalid_response; | ||
421 | } | ||
422 | |||
354 | cursor = rjs.at("next_cursor"); | 423 | cursor = rjs.at("next_cursor"); |
355 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); | 424 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); |
356 | } else { | 425 | } else { |
@@ -392,7 +461,14 @@ namespace twitter { | |||
392 | 461 | ||
393 | if (response_code == 200) | 462 | if (response_code == 200) |
394 | { | 463 | { |
395 | json rjs = json::parse(response_data); | 464 | json rjs; |
465 | try { | ||
466 | rjs = json::parse(response_data); | ||
467 | } catch (std::invalid_argument e) | ||
468 | { | ||
469 | return response::invalid_response; | ||
470 | } | ||
471 | |||
396 | cursor = rjs.at("next_cursor"); | 472 | cursor = rjs.at("next_cursor"); |
397 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); | 473 | result.insert(std::begin(rjs.at("ids")), std::end(rjs.at("ids"))); |
398 | } else { | 474 | } else { |
@@ -544,7 +620,13 @@ namespace twitter { | |||
544 | 620 | ||
545 | response client::codeForError(int response_code, std::string response_data) const | 621 | response client::codeForError(int response_code, std::string response_data) const |
546 | { | 622 | { |
547 | auto response_json = json::parse(response_data); | 623 | json response_json; |
624 | try { | ||
625 | response_json = json::parse(response_data); | ||
626 | } catch (std::invalid_argument e) | ||
627 | { | ||
628 | return response::invalid_response; | ||
629 | } | ||
548 | 630 | ||
549 | std::set<int> error_codes; | 631 | std::set<int> error_codes; |
550 | if (response_json.find("errors") != response_json.end()) | 632 | if (response_json.find("errors") != response_json.end()) |