blob: c10805a3c9dd8df396c2b788dfed5395febfdee7 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -07001// Copyright 2014 The Chromium OS 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#ifndef BUFFET_HTTP_UTILS_H_
6#define BUFFET_HTTP_UTILS_H_
7
8#include "buffet/http_request.h"
9
Alex Vakulenkob645cc92014-04-15 11:34:35 -070010namespace base {
11 class Value;
12 class DictionaryValue;
13} // namespace base
Chris Sosa45d9f102014-03-24 11:18:54 -070014
15namespace chromeos {
16namespace http {
17
Alex Vakulenkob645cc92014-04-15 11:34:35 -070018typedef std::vector<std::pair<std::string, std::string>> FormFieldList;
19
Chris Sosa45d9f102014-03-24 11:18:54 -070020////////////////////////////////////////////////////////////////////////////////
21// The following are simple utility helper functions for common HTTP operations
22// that use http::Request object behind the scenes and set it up accordingly.
23//
24// For more advanced functionality you need to use Request/Response objects
25// directly.
26////////////////////////////////////////////////////////////////////////////////
27
Alex Vakulenkob645cc92014-04-15 11:34:35 -070028// Performs a generic HTTP request with binary data. Success status,
29// returned data and additional information (such as returned HTTP headers)
30// can be obtained from the returned Response object.
31// If data MIME type is not specified, "application/octet-stream" is assumed.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070032std::unique_ptr<Response> SendRequest(
33 const char* method, const std::string& url,
34 const void* data, size_t data_size, const char* mime_type,
35 const HeaderList& headers, std::shared_ptr<Transport> transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070036
Chris Sosa45d9f102014-03-24 11:18:54 -070037// Performs a simple GET request and returns the data as a string.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070038std::string GetAsString(const std::string& url, const HeaderList& headers,
39 std::shared_ptr<Transport> transport);
40inline std::string GetAsString(const std::string& url,
41 std::shared_ptr<Transport> transport) {
42 return GetAsString(url, HeaderList(), transport);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070043}
Chris Sosa45d9f102014-03-24 11:18:54 -070044
45// Performs a GET request. Success status, returned data and additional
46// information (such as returned HTTP headers) can be obtained from
47// the returned Response object.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070048std::unique_ptr<Response> Get(const std::string& url,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070049 const HeaderList& headers,
50 std::shared_ptr<Transport> transport);
51inline std::unique_ptr<Response> Get(
52 const std::string& url, std::shared_ptr<Transport> transport) {
53 return Get(url, HeaderList(), transport);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070054}
Chris Sosa45d9f102014-03-24 11:18:54 -070055
56// Performs a HEAD request. Success status and additional
57// information (such as returned HTTP headers) can be obtained from
58// the returned Response object.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070059std::unique_ptr<Response> Head(const std::string& url,
60 std::shared_ptr<Transport> transport);
Chris Sosa45d9f102014-03-24 11:18:54 -070061
62// Performs a POST request with binary data. Success status, returned data
63// and additional information (such as returned HTTP headers) can be obtained
64// from the returned Response object.
65// If data MIME type is not specified, "application/octet-stream" is assumed
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070066std::unique_ptr<Response> PostBinary(const std::string& url,
67 const void* data,
Chris Sosa45d9f102014-03-24 11:18:54 -070068 size_t data_size,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070069 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070070 const HeaderList& headers,
71 std::shared_ptr<Transport> transport);
Chris Sosa45d9f102014-03-24 11:18:54 -070072
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070073inline std::unique_ptr<Response> PostBinary(
74 const std::string& url, const void* data, size_t data_size,
75 const char* mime_type, std::shared_ptr<Transport> transport) {
76 return PostBinary(url, data, data_size, mime_type, HeaderList(), transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070077}
78
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070079inline std::unique_ptr<Response> PostBinary(
80 const std::string& url, const void* data, size_t data_size,
81 std::shared_ptr<Transport> transport) {
82 return PostBinary(url, data, data_size, nullptr, transport);
Chris Sosa45d9f102014-03-24 11:18:54 -070083}
84
85// Performs a POST request with text data. Success status, returned data
86// and additional information (such as returned HTTP headers) can be obtained
87// from the returned Response object.
88// If data MIME type is not specified, "application/x-www-form-urlencoded"
Alex Vakulenkob645cc92014-04-15 11:34:35 -070089// is assumed.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070090std::unique_ptr<Response> PostText(const std::string& url,
91 const char* data,
92 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070093 const HeaderList& headers,
94 std::shared_ptr<Transport> transport);
Chris Sosa45d9f102014-03-24 11:18:54 -070095
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070096inline std::unique_ptr<Response> PostText(
97 const std::string& url, const char* data, const char* mime_type,
98 std::shared_ptr<Transport> transport) {
99 return PostText(url, data, mime_type, HeaderList(), transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700100}
101
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700102inline std::unique_ptr<Response> PostText(
103 const std::string& url, const char* data,
104 std::shared_ptr<Transport> transport) {
105 return PostText(url, data, nullptr, transport);
Chris Sosa45d9f102014-03-24 11:18:54 -0700106}
107
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700108// Performs a POST request with form data. Success status, returned data
109// and additional information (such as returned HTTP headers) can be obtained
110// from the returned Response object. The form data is a list of key/value
111// pairs. The data is posed as "application/x-www-form-urlencoded".
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700112std::unique_ptr<Response> PostFormData(
113 const std::string& url, const FormFieldList& data,
114 const HeaderList& headers, std::shared_ptr<Transport> transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700115
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700116inline std::unique_ptr<Response> PostFormData(
117 const std::string& url, const FormFieldList& data,
118 std::shared_ptr<Transport> transport) {
119 return PostFormData(url, data, HeaderList(), transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700120}
121
Chris Sosa45d9f102014-03-24 11:18:54 -0700122// Performs a POST request with JSON data. Success status, returned data
123// and additional information (such as returned HTTP headers) can be obtained
124// from the returned Response object. If a JSON response is expected,
125// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700126std::unique_ptr<Response> PostJson(const std::string& url,
127 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700128 const HeaderList& headers,
129 std::shared_ptr<Transport> transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700130
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700131inline std::unique_ptr<Response> PostJson(
132 const std::string& url, const base::Value* json,
133 std::shared_ptr<Transport> transport) {
134 return PostJson(url, json, HeaderList(), transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700135}
136
137// Performs a PATCH request with JSON data. Success status, returned data
138// and additional information (such as returned HTTP headers) can be obtained
139// from the returned Response object. If a JSON response is expected,
140// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700141std::unique_ptr<Response> PatchJson(const std::string& url,
142 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700143 const HeaderList& headers,
144 std::shared_ptr<Transport> transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700145
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700146inline std::unique_ptr<Response> PatchJson(
147 const std::string& url, const base::Value* json,
148 std::shared_ptr<Transport> transport) {
149 return PatchJson(url, json, HeaderList(), transport);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700150}
Chris Sosa45d9f102014-03-24 11:18:54 -0700151
152// Given an http::Response object, parse the body data into Json object.
153// Returns null if failed. Optional |error_message| can be passed in to
154// get the extended error information as to why the parse failed.
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700155std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700156 const Response* response, int* status_code, std::string* error_message);
Chris Sosa45d9f102014-03-24 11:18:54 -0700157
158} // namespace http
159} // namespace chromeos
160
161#endif // BUFFET_HTTP_UTILS_H_