OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 | |
5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "components/proximity_auth/logging/logging.h" | |
9 #include "net/url_request/url_fetcher.h" | |
10 | |
11 namespace proximity_auth { | |
12 | |
13 namespace { | |
14 | |
15 const char kResponseBodyError[] = "Failed to get response body"; | |
16 const char kRequestFailedError[] = "Request failed"; | |
17 const char kHttpStatusErrorPrefix[] = "HTTP status: "; | |
18 | |
19 } // namespace | |
20 | |
21 CryptAuthApiCallFlow::CryptAuthApiCallFlow() { | |
22 } | |
23 | |
24 CryptAuthApiCallFlow::~CryptAuthApiCallFlow() { | |
25 } | |
26 | |
27 void CryptAuthApiCallFlow::Start(const GURL& request_url, | |
28 net::URLRequestContextGetter* context, | |
29 const std::string& access_token, | |
30 const std::string& serialized_request, | |
31 const ResultCallback& result_callback, | |
32 const ErrorCallback& error_callback) { | |
33 request_url_ = request_url; | |
34 serialized_request_ = serialized_request; | |
35 result_callback_ = result_callback; | |
36 error_callback_ = error_callback; | |
37 OAuth2ApiCallFlow::Start(context, access_token); | |
38 } | |
39 | |
40 GURL CryptAuthApiCallFlow::CreateApiCallUrl() { | |
41 return request_url_; | |
42 } | |
43 | |
44 std::string CryptAuthApiCallFlow::CreateApiCallBody() { | |
45 return serialized_request_; | |
46 } | |
47 | |
48 std::string CryptAuthApiCallFlow::CreateApiCallBodyContentType() { | |
49 return "application/x-protobuf"; | |
50 } | |
51 | |
52 net::URLFetcher::RequestType CryptAuthApiCallFlow::GetRequestTypeForBody( | |
53 const std::string& body) { | |
54 return net::URLFetcher::POST; | |
55 } | |
56 | |
57 void CryptAuthApiCallFlow::ProcessApiCallSuccess( | |
58 const net::URLFetcher* source) { | |
59 std::string serialized_response; | |
60 if (!source->GetResponseAsString(&serialized_response)) { | |
61 error_callback_.Run(kResponseBodyError); | |
62 return; | |
63 } | |
64 result_callback_.Run(serialized_response); | |
65 } | |
66 | |
67 void CryptAuthApiCallFlow::ProcessApiCallFailure( | |
68 const net::URLFetcher* source) { | |
69 std::string error_message; | |
70 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) { | |
71 error_message = | |
72 kHttpStatusErrorPrefix + base::IntToString(source->GetResponseCode()); | |
73 } else { | |
74 error_message = kRequestFailedError; | |
75 } | |
76 | |
77 std::string response; | |
78 source->GetResponseAsString(&response); | |
79 PA_LOG(INFO) << "API call failed:\n" << response; | |
80 error_callback_.Run(error_message); | |
81 } | |
82 | |
83 } // proximity_auth | |
OLD | NEW |