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.
5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "components/proximity_auth/logging/logging.h"
9 #include "net/url_request/url_fetcher.h"
11 namespace proximity_auth
{
15 const char kResponseBodyError
[] = "Failed to get response body";
16 const char kRequestFailedError
[] = "Request failed";
17 const char kHttpStatusErrorPrefix
[] = "HTTP status: ";
21 CryptAuthApiCallFlow::CryptAuthApiCallFlow() {
24 CryptAuthApiCallFlow::~CryptAuthApiCallFlow() {
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
);
40 GURL
CryptAuthApiCallFlow::CreateApiCallUrl() {
44 std::string
CryptAuthApiCallFlow::CreateApiCallBody() {
45 return serialized_request_
;
48 std::string
CryptAuthApiCallFlow::CreateApiCallBodyContentType() {
49 return "application/x-protobuf";
52 net::URLFetcher::RequestType
CryptAuthApiCallFlow::GetRequestTypeForBody(
53 const std::string
& body
) {
54 return net::URLFetcher::POST
;
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
);
64 result_callback_
.Run(serialized_response
);
67 void CryptAuthApiCallFlow::ProcessApiCallFailure(
68 const net::URLFetcher
* source
) {
69 std::string error_message
;
70 if (source
->GetStatus().status() == net::URLRequestStatus::SUCCESS
) {
72 kHttpStatusErrorPrefix
+ base::IntToString(source
->GetResponseCode());
74 error_message
= kRequestFailedError
;
78 source
->GetResponseAsString(&response
);
79 PA_LOG(INFO
) << "API call failed:\n" << response
;
80 error_callback_
.Run(error_message
);