calendar_api_requests.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "google_apis/calendar/calendar_api_requests.h"
  5. #include <stddef.h>
  6. #include "base/task/task_runner_util.h"
  7. #include "base/values.h"
  8. #include "net/base/url_util.h"
  9. namespace google_apis {
  10. namespace calendar {
  11. constexpr char kFieldsParameterName[] = "fields";
  12. // According to the docs
  13. // (https://developers.google.com/calendar/api/v3/reference/events/list), it
  14. // should return the participant/requester only as an attendee.
  15. constexpr int kMaxAttendees = 1;
  16. constexpr char kCalendarEventListFields[] =
  17. "timeZone,etag,kind,items(id,kind,summary,colorId,"
  18. "status,start(date),end(date),start(dateTime),end(dateTime),htmlLink,"
  19. "attendees(responseStatus,self),attendeesOmitted,creator(self))";
  20. CalendarApiGetRequest::CalendarApiGetRequest(RequestSender* sender,
  21. const std::string& fields)
  22. : UrlFetchRequestBase(sender, ProgressCallback(), ProgressCallback()),
  23. fields_(fields) {}
  24. CalendarApiGetRequest::~CalendarApiGetRequest() = default;
  25. GURL CalendarApiGetRequest::GetURL() const {
  26. GURL url = GetURLInternal();
  27. if (!fields_.empty()) {
  28. url =
  29. net::AppendOrReplaceQueryParameter(url, kFieldsParameterName, fields_);
  30. }
  31. return url;
  32. }
  33. // Maps calendar api error reason to code. See
  34. // https://developers.google.com/calendar/api/guides/errors.
  35. ApiErrorCode CalendarApiGetRequest::MapReasonToError(
  36. ApiErrorCode code,
  37. const std::string& reason) {
  38. const char kErrorReasonRateLimitExceeded[] = "rateLimitExceeded";
  39. // The rateLimitExceeded errors can return either 403 or 429 error codes, but
  40. // we want to treat them in the same way.
  41. if (reason == kErrorReasonRateLimitExceeded)
  42. return google_apis::HTTP_FORBIDDEN;
  43. return code;
  44. }
  45. bool CalendarApiGetRequest::IsSuccessfulErrorCode(ApiErrorCode error) {
  46. return IsSuccessfulCalendarApiErrorCode(error);
  47. }
  48. CalendarApiEventsRequest::CalendarApiEventsRequest(
  49. RequestSender* sender,
  50. const CalendarApiUrlGenerator& url_generator,
  51. CalendarEventListCallback callback,
  52. const base::Time& start_time,
  53. const base::Time& end_time)
  54. : CalendarApiGetRequest(sender, kCalendarEventListFields),
  55. callback_(std::move(callback)),
  56. url_generator_(url_generator),
  57. start_time_(start_time),
  58. end_time_(end_time) {
  59. DCHECK(!callback_.is_null());
  60. }
  61. CalendarApiEventsRequest::~CalendarApiEventsRequest() = default;
  62. GURL CalendarApiEventsRequest::GetURLInternal() const {
  63. return url_generator_.GetCalendarEventListUrl(
  64. start_time_, end_time_,
  65. /*single_events=*/true,
  66. /*max_attendees=*/kMaxAttendees);
  67. }
  68. void CalendarApiEventsRequest::ProcessURLFetchResults(
  69. const network::mojom::URLResponseHead* response_head,
  70. base::FilePath response_file,
  71. std::string response_body) {
  72. ApiErrorCode error = GetErrorCode();
  73. switch (error) {
  74. case HTTP_SUCCESS:
  75. base::PostTaskAndReplyWithResult(
  76. blocking_task_runner(), FROM_HERE,
  77. base::BindOnce(&CalendarApiEventsRequest::Parse,
  78. std::move(response_body)),
  79. base::BindOnce(&CalendarApiEventsRequest::OnDataParsed,
  80. weak_ptr_factory_.GetWeakPtr(), error));
  81. break;
  82. default:
  83. RunCallbackOnPrematureFailure(error);
  84. OnProcessURLFetchResultsComplete();
  85. break;
  86. }
  87. }
  88. void CalendarApiEventsRequest::OnDataParsed(ApiErrorCode error,
  89. std::unique_ptr<EventList> events) {
  90. if (!events)
  91. error = PARSE_ERROR;
  92. std::move(callback_).Run(error, std::move(events));
  93. OnProcessURLFetchResultsComplete();
  94. }
  95. void CalendarApiEventsRequest::RunCallbackOnPrematureFailure(
  96. ApiErrorCode error) {
  97. std::move(callback_).Run(error, std::unique_ptr<EventList>());
  98. }
  99. // static
  100. std::unique_ptr<EventList> CalendarApiEventsRequest::Parse(std::string json) {
  101. std::unique_ptr<base::Value> value = ParseJson(json);
  102. return value ? EventList::CreateFrom(*value) : nullptr;
  103. }
  104. } // namespace calendar
  105. } // namespace google_apis