Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // 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 | |
| 10 | namespace base { class Value; } |
| 11 | |
| 12 | namespace chromeos { |
| 13 | namespace http { |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | // The following are simple utility helper functions for common HTTP operations |
| 17 | // that use http::Request object behind the scenes and set it up accordingly. |
| 18 | // |
| 19 | // For more advanced functionality you need to use Request/Response objects |
| 20 | // directly. |
| 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | // Performs a simple GET request and returns the data as a string. |
| 24 | std::string GetAsString(char const* url); |
| 25 | |
| 26 | // Performs a GET request. Success status, returned data and additional |
| 27 | // information (such as returned HTTP headers) can be obtained from |
| 28 | // the returned Response object. |
| 29 | std::unique_ptr<Response> Get(char const* url); |
| 30 | |
| 31 | // Performs a HEAD request. Success status and additional |
| 32 | // information (such as returned HTTP headers) can be obtained from |
| 33 | // the returned Response object. |
| 34 | std::unique_ptr<Response> Head(char const* url); |
| 35 | |
| 36 | // Performs a POST request with binary data. Success status, returned data |
| 37 | // and additional information (such as returned HTTP headers) can be obtained |
| 38 | // from the returned Response object. |
| 39 | // If data MIME type is not specified, "application/octet-stream" is assumed |
| 40 | std::unique_ptr<Response> PostBinary(char const* url, |
| 41 | void const* data, |
| 42 | size_t data_size, |
| 43 | char const* mime_type); |
| 44 | |
| 45 | inline std::unique_ptr<Response> PostBinary(char const* url, |
| 46 | void const* data, |
| 47 | size_t data_size) { |
| 48 | return PostBinary(url, data, data_size, nullptr); |
| 49 | } |
| 50 | |
| 51 | // Performs a POST request with text data. Success status, returned data |
| 52 | // and additional information (such as returned HTTP headers) can be obtained |
| 53 | // from the returned Response object. |
| 54 | // If data MIME type is not specified, "application/x-www-form-urlencoded" |
| 55 | // is assumed |
| 56 | std::unique_ptr<Response> PostText(char const* url, |
| 57 | char const* data, |
| 58 | char const* mime_type); |
| 59 | |
| 60 | inline std::unique_ptr<Response> PostText(char const* url, char const* data) { |
| 61 | return PostText(url, data, nullptr); |
| 62 | } |
| 63 | |
| 64 | // Performs a POST request with JSON data. Success status, returned data |
| 65 | // and additional information (such as returned HTTP headers) can be obtained |
| 66 | // from the returned Response object. If a JSON response is expected, |
| 67 | // use ParseJsonResponse() method on the returned Response object. |
| 68 | std::unique_ptr<Response> PostJson(char const* url, base::Value const* json); |
| 69 | |
| 70 | // Given an http::Response object, parse the body data into Json object. |
| 71 | // Returns null if failed. Optional |error_message| can be passed in to |
| 72 | // get the extended error information as to why the parse failed. |
| 73 | std::unique_ptr<base::Value> ParseJsonResponse(Response const* response, |
| 74 | std::string* error_message); |
| 75 | |
| 76 | } // namespace http |
| 77 | } // namespace chromeos |
| 78 | |
| 79 | #endif // BUFFET_HTTP_UTILS_H_ |