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 "net/url_request/url_fetcher.h"
10 namespace proximity_auth
{
14 const char kResponseBodyError
[] = "Failed to get response body";
15 const char kRequestFailedError
[] = "Request failed";
16 const char kHttpStatusErrorPrefix
[] = "HTTP status: ";
20 CryptAuthApiCallFlow::CryptAuthApiCallFlow() {
23 CryptAuthApiCallFlow::~CryptAuthApiCallFlow() {
26 void CryptAuthApiCallFlow::Start(const GURL
& request_url
,
27 net::URLRequestContextGetter
* context
,
28 const std::string
& access_token
,
29 const std::string
& serialized_request
,
30 const ResultCallback
& result_callback
,
31 const ErrorCallback
& error_callback
) {
32 request_url_
= request_url
;
33 serialized_request_
= serialized_request
;
34 result_callback_
= result_callback
;
35 error_callback_
= error_callback
;
36 OAuth2ApiCallFlow::Start(context
, access_token
);
39 GURL
CryptAuthApiCallFlow::CreateApiCallUrl() {
43 std::string
CryptAuthApiCallFlow::CreateApiCallBody() {
44 return serialized_request_
;
47 std::string
CryptAuthApiCallFlow::CreateApiCallBodyContentType() {
48 return "application/x-protobuf";
51 void CryptAuthApiCallFlow::ProcessApiCallSuccess(
52 const net::URLFetcher
* source
) {
53 std::string serialized_response
;
54 if (!source
->GetResponseAsString(&serialized_response
)) {
55 error_callback_
.Run(kResponseBodyError
);
58 result_callback_
.Run(serialized_response
);
61 void CryptAuthApiCallFlow::ProcessApiCallFailure(
62 const net::URLFetcher
* source
) {
63 std::string error_message
;
64 if (source
->GetStatus().status() == net::URLRequestStatus::SUCCESS
) {
66 kHttpStatusErrorPrefix
+ base::IntToString(source
->GetResponseCode());
68 error_message
= kRequestFailedError
;
70 error_callback_
.Run(error_message
);