Android-cuttlefish cvd tool
http_json.h
Go to the documentation of this file.
1//
2// Copyright (C) 2019 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <string>
19#include <vector>
20
21#include "absl/strings/string_view.h"
22#include "json/json.h"
23
26
27namespace cuttlefish {
28
29// Returns the json object contained in the response's body.
30//
31// NOTE: In case of a parsing error a successful `result` will be returned
32// with the relevant http status code and a json object with the next format:
33// {
34// "error": "Failed to parse json",
35// "response: "<THE RESPONSE BODY>"
36// }
37Result<HttpResponse<Json::Value>> HttpPostToJson(
38 HttpClient&, const std::string& url, const std::string& data,
39 const std::vector<std::string>& headers = {});
40Result<HttpResponse<Json::Value>> HttpPostToJson(
41 HttpClient&, const std::string& url, const Json::Value& data,
42 const std::vector<std::string>& headers = {});
43Result<HttpResponse<Json::Value>> HttpGetToJson(
44 HttpClient&, const std::string& url,
45 const std::vector<std::string>& headers = {});
46
47// What differs between the APIs that answer a request with json.
49 // Names the API in the error of a response that failed.
50 absl::string_view source;
51 // Whether a redirect carries the json the caller asked for.
52 bool allow_redirect = false;
53 // Whether an "error" member of a successful response is itself a failure.
54 bool reject_error_member = false;
55 // Appended to the error of a response that failed.
56 absl::string_view hint;
57};
58
59// Logs the body of `response` and returns its json, or an error naming the
60// status the API answered with.
62 const JsonResponseOptions& options);
63
64} // namespace cuttlefish
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_driver.h:21
Result< HttpResponse< Json::Value > > HttpPostToJson(HttpClient &http_client, const std::string &url, const std::string &data, const std::vector< std::string > &headers)
Definition: http_json.cc:53
Result< HttpResponse< Json::Value > > HttpGetToJson(HttpClient &http_client, const std::string &url, const std::vector< std::string > &headers)
Definition: http_json.cc:68
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
Result< Json::Value > JsonFromResponse(const HttpResponse< Json::Value > &response, const JsonResponseOptions &options)
Definition: http_json.cc:74
Definition: http_client.h:42
Definition: http_json.h:48
absl::string_view source
Definition: http_json.h:50
absl::string_view hint
Definition: http_json.h:56
bool allow_redirect
Definition: http_json.h:52
bool reject_error_member
Definition: http_json.h:54