Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / google_apis / drive / drive_api_url_generator.cc
blobb8374e2769940e349a2efe05e58617e442dd4cee
1 // Copyright (c) 2012 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.
5 #include "google_apis/drive/drive_api_url_generator.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "google_apis/google_api_keys.h"
11 #include "net/base/escape.h"
12 #include "net/base/url_util.h"
14 namespace google_apis {
16 namespace {
18 // Hard coded URLs for communication with a google drive server.
19 const char kDriveV2AboutUrl[] = "/drive/v2/about";
20 const char kDriveV2AppsUrl[] = "/drive/v2/apps";
21 const char kDriveV2ChangelistUrl[] = "/drive/v2/changes";
22 const char kDriveV2FilesUrl[] = "/drive/v2/files";
23 const char kDriveV2FileUrlPrefix[] = "/drive/v2/files/";
24 const char kDriveV2ChildrenUrlFormat[] = "/drive/v2/files/%s/children";
25 const char kDriveV2ChildrenUrlForRemovalFormat[] =
26 "/drive/v2/files/%s/children/%s";
27 const char kDriveV2FileCopyUrlFormat[] = "/drive/v2/files/%s/copy";
28 const char kDriveV2FileDeleteUrlFormat[] = "/drive/v2/files/%s";
29 const char kDriveV2FileTrashUrlFormat[] = "/drive/v2/files/%s/trash";
30 const char kDriveV2InitiateUploadNewFileUrl[] = "/upload/drive/v2/files";
31 const char kDriveV2InitiateUploadExistingFileUrlPrefix[] =
32 "/upload/drive/v2/files/";
33 const char kDriveV2PermissionsUrlFormat[] = "/drive/v2/files/%s/permissions";
35 // apps.delete and file.authorize API is exposed through a special endpoint
36 // v2internal that is accessible only by the official API key for Chrome.
37 const char kDriveV2InternalAppsUrl[] = "/drive/v2internal/apps";
38 const char kDriveV2AppsDeleteUrlFormat[] = "/drive/v2internal/apps/%s";
39 const char kDriveV2FilesAuthorizeUrlFormat[] =
40 "/drive/v2internal/files/%s/authorize?appId=%s";
42 GURL AddResumableUploadParam(const GURL& url) {
43 return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
46 } // namespace
48 DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url,
49 const GURL& base_download_url)
50 : base_url_(base_url),
51 base_download_url_(base_download_url) {
52 // Do nothing.
55 DriveApiUrlGenerator::~DriveApiUrlGenerator() {
56 // Do nothing.
59 const char DriveApiUrlGenerator::kBaseUrlForProduction[] =
60 "https://www.googleapis.com";
61 const char DriveApiUrlGenerator::kBaseDownloadUrlForProduction[] =
62 "https://www.googledrive.com/host/";
64 GURL DriveApiUrlGenerator::GetAboutGetUrl() const {
65 return base_url_.Resolve(kDriveV2AboutUrl);
68 GURL DriveApiUrlGenerator::GetAppsListUrl(bool use_internal_endpoint) const {
69 return base_url_.Resolve(use_internal_endpoint ?
70 kDriveV2InternalAppsUrl : kDriveV2AppsUrl);
73 GURL DriveApiUrlGenerator::GetAppsDeleteUrl(const std::string& app_id) const {
74 return base_url_.Resolve(base::StringPrintf(
75 kDriveV2AppsDeleteUrlFormat, net::EscapePath(app_id).c_str()));
78 GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id) const {
79 return base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
82 GURL DriveApiUrlGenerator::GetFilesAuthorizeUrl(
83 const std::string& file_id,
84 const std::string& app_id) const {
85 return base_url_.Resolve(base::StringPrintf(kDriveV2FilesAuthorizeUrlFormat,
86 net::EscapePath(file_id).c_str(),
87 net::EscapePath(app_id).c_str()));
90 GURL DriveApiUrlGenerator::GetFilesInsertUrl() const {
91 return base_url_.Resolve(kDriveV2FilesUrl);
94 GURL DriveApiUrlGenerator::GetFilesPatchUrl(const std::string& file_id,
95 bool set_modified_date,
96 bool update_viewed_date) const {
97 GURL url =
98 base_url_.Resolve(kDriveV2FileUrlPrefix + net::EscapePath(file_id));
100 // setModifiedDate is "false" by default.
101 if (set_modified_date)
102 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
104 // updateViewedDate is "true" by default.
105 if (!update_viewed_date)
106 url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");
108 return url;
111 GURL DriveApiUrlGenerator::GetFilesCopyUrl(const std::string& file_id) const {
112 return base_url_.Resolve(base::StringPrintf(
113 kDriveV2FileCopyUrlFormat, net::EscapePath(file_id).c_str()));
116 GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
117 const std::string& page_token,
118 const std::string& q) const {
119 GURL url = base_url_.Resolve(kDriveV2FilesUrl);
121 // maxResults is 100 by default.
122 if (max_results != 100) {
123 url = net::AppendOrReplaceQueryParameter(
124 url, "maxResults", base::IntToString(max_results));
127 if (!page_token.empty())
128 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
130 if (!q.empty())
131 url = net::AppendOrReplaceQueryParameter(url, "q", q);
133 return url;
136 GURL DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string& file_id) const {
137 return base_url_.Resolve(base::StringPrintf(
138 kDriveV2FileDeleteUrlFormat, net::EscapePath(file_id).c_str()));
141 GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
142 return base_url_.Resolve(base::StringPrintf(
143 kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str()));
146 GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted,
147 int max_results,
148 const std::string& page_token,
149 int64 start_change_id) const {
150 DCHECK_GE(start_change_id, 0);
152 GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
154 // includeDeleted is "true" by default.
155 if (!include_deleted)
156 url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
158 // maxResults is "100" by default.
159 if (max_results != 100) {
160 url = net::AppendOrReplaceQueryParameter(
161 url, "maxResults", base::IntToString(max_results));
164 if (!page_token.empty())
165 url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
167 if (start_change_id > 0)
168 url = net::AppendOrReplaceQueryParameter(
169 url, "startChangeId", base::Int64ToString(start_change_id));
171 return url;
174 GURL DriveApiUrlGenerator::GetChildrenInsertUrl(
175 const std::string& file_id) const {
176 return base_url_.Resolve(base::StringPrintf(
177 kDriveV2ChildrenUrlFormat, net::EscapePath(file_id).c_str()));
180 GURL DriveApiUrlGenerator::GetChildrenDeleteUrl(
181 const std::string& child_id, const std::string& folder_id) const {
182 return base_url_.Resolve(
183 base::StringPrintf(kDriveV2ChildrenUrlForRemovalFormat,
184 net::EscapePath(folder_id).c_str(),
185 net::EscapePath(child_id).c_str()));
188 GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
189 bool set_modified_date) const {
190 GURL url = AddResumableUploadParam(
191 base_url_.Resolve(kDriveV2InitiateUploadNewFileUrl));
193 // setModifiedDate is "false" by default.
194 if (set_modified_date)
195 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
197 return url;
200 GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
201 const std::string& resource_id,
202 bool set_modified_date) const {
203 GURL url = base_url_.Resolve(
204 kDriveV2InitiateUploadExistingFileUrlPrefix +
205 net::EscapePath(resource_id));
206 url = AddResumableUploadParam(url);
208 // setModifiedDate is "false" by default.
209 if (set_modified_date)
210 url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
212 return url;
215 GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
216 const std::string& resource_id) const {
217 return base_download_url_.Resolve(net::EscapePath(resource_id));
220 GURL DriveApiUrlGenerator::GetPermissionsInsertUrl(
221 const std::string& resource_id) const {
222 return base_url_.Resolve(
223 base::StringPrintf(kDriveV2PermissionsUrlFormat,
224 net::EscapePath(resource_id).c_str()));
227 } // namespace google_apis