RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* http_rest_proxy.h -*- C++ -*- 00002 Jeremy Barnes, 10 April 2013 00003 Copyright (c) 2013 Datacratic Inc. All rights reserved. 00004 00005 */ 00006 00007 #pragma once 00008 00009 #include "soa/service/http_endpoint.h" 00010 #include "jml/utils/vector_utils.h" 00011 #include <boost/make_shared.hpp> 00012 00013 00014 namespace Datacratic { 00015 00016 00017 /*****************************************************************************/ 00018 /* HTTP REST PROXY */ 00019 /*****************************************************************************/ 00020 00025 struct HttpRestProxy { 00026 00027 HttpRestProxy(const std::string & serviceUri = "") 00028 : serviceUri(serviceUri) 00029 { 00030 } 00031 00033 struct Response { 00034 Response() 00035 : code_(0) 00036 { 00037 } 00038 00040 int code() const { 00041 return code_; 00042 } 00043 00045 std::string body() const 00046 { 00047 if (code_ < 200 || code_ >= 300) 00048 throw ML::Exception("invalid http code returned"); 00049 return body_; 00050 } 00051 00053 std::string getHeader(const std::string & name) const 00054 { 00055 auto it = header_.headers.find(name); 00056 if (it == header_.headers.end()) 00057 throw ML::Exception("required header " + name + " not found"); 00058 return it->second; 00059 } 00060 00061 long code_; 00062 std::string body_; 00063 HttpHeader header_; 00064 }; 00065 00067 struct Content { 00068 Content() 00069 : data(0), size(0), hasContent(false) 00070 { 00071 } 00072 00073 Content(const std::string & str, 00074 const std::string & contentType = "") 00075 : str(str), data(str.c_str()), size(str.size()), 00076 hasContent(true), contentType(contentType) 00077 { 00078 } 00079 00080 Content(const char * data, uint64_t size, 00081 const std::string & contentType = "", 00082 const std::string & contentMd5 = "") 00083 : data(data), size(size), hasContent(true), 00084 contentType(contentType), contentMd5(contentMd5) 00085 { 00086 } 00087 00088 std::string str; 00089 00090 const char * data; 00091 uint64_t size; 00092 bool hasContent; 00093 00094 std::string contentType; 00095 std::string contentMd5; 00096 }; 00097 00099 Response post(const std::string & resource, 00100 const Content & content = Content(), 00101 const RestParams & queryParams = RestParams(), 00102 const RestParams & headers = RestParams(), 00103 int timeout = -1) const 00104 { 00105 return perform("POST", resource, content, queryParams, headers, 00106 timeout); 00107 } 00108 00110 Response get(const std::string & resource, 00111 const RestParams & queryParams = RestParams(), 00112 const RestParams & headers = RestParams(), 00113 int timeout = -1) const 00114 { 00115 return perform("GET", resource, Content(), queryParams, headers, 00116 timeout); 00117 } 00118 00120 Response perform(const std::string & verb, 00121 const std::string & resource, 00122 const Content & content = Content(), 00123 const RestParams & queryParams = RestParams(), 00124 const RestParams & headers = RestParams(), 00125 int timeout = -1) const; 00126 00130 std::string serviceUri; 00131 }; 00132 00133 } // namespace Datacratic 00134