2020
2121using namespace RestClient ;
2222
23- Connection::Connection (const std::string baseUrl) {
23+ Connection::Connection (const std::string baseUrl) : infoStruct(), headerFields() {
2424 this ->curlHandle = curl_easy_init ();
2525 if (!this ->curlHandle ) {
2626 throw std::runtime_error (" Couldn't initialize curl handle" );
2727 }
2828 this ->baseUrl = baseUrl;
29- this ->infoStruct = new Info ();
3029}
3130
3231Connection::~Connection () {
@@ -37,6 +36,21 @@ Connection::~Connection() {
3736
3837// getters/setters
3938
39+ /* *
40+ * @brief append a header to the internal map
41+ *
42+ * @param key for the header field
43+ * @param value for the header field
44+ *
45+ */
46+ void Connection::AppendHeader (const std::string& key,
47+ const std::string& value) {
48+
49+ this ->headerFields [key] = value;
50+ }
51+
52+
53+
4054
4155/* *
4256 * @brief helper function to get called from the actual request methods to
@@ -51,8 +65,9 @@ Connection::~Connection() {
5165 *
5266 * @return 0 on success and 1 on error
5367 */
54- int Connection::performCurlRequest (const std::string& uri,
55- RestClient::Response& ret) {
68+ Response Connection::performCurlRequest (const std::string& uri) {
69+
70+ Response ret = {};
5671
5772 std::string url = std::string (this ->baseUrl + uri);
5873 std::string headerString;
@@ -106,7 +121,7 @@ int Connection::performCurlRequest(const std::string& uri,
106121 curl_slist_free_all (headerList);
107122 // reset curl handle
108123 curl_easy_reset (this ->curlHandle );
109- return 0 ;
124+ return ret ;
110125}
111126
112127/* *
@@ -117,12 +132,7 @@ int Connection::performCurlRequest(const std::string& uri,
117132 * @return response struct
118133 */
119134Response Connection::get (const std::string& url) {
120- /* * create return struct */
121- Response ret = {};
122- CURLcode res = CURLE_OK;
123-
124- this ->performCurlRequest (url, &ret);
125- return ret;
135+ return this ->performCurlRequest (url);
126136}
127137/* *
128138 * @brief HTTP POST method
@@ -135,17 +145,13 @@ Response Connection::get(const std::string& url) {
135145 */
136146Response Connection::post (const std::string& url,
137147 const std::string& data) {
138- /* * create return struct */
139- RestClient::Response ret = {};
140-
141148 /* * Now specify we want to POST data */
142149 curl_easy_setopt (this ->curlHandle , CURLOPT_POST, 1L );
143150 /* * set post fields */
144151 curl_easy_setopt (this ->curlHandle , CURLOPT_POSTFIELDS, data.c_str ());
145152 curl_easy_setopt (this ->curlHandle , CURLOPT_POSTFIELDSIZE, data.size ());
146153
147- this ->performCurlRequest (url, &ret);
148- return ret;
154+ return this ->performCurlRequest (url);
149155}
150156/* *
151157 * @brief HTTP PUT method
@@ -156,12 +162,8 @@ Response Connection::post(const std::string& url,
156162 *
157163 * @return response struct
158164 */
159- RestClient:: Response Connection::put (const std::string& url,
165+ Response Connection::put (const std::string& url,
160166 const std::string& data) {
161- /* * create return struct */
162- RestClient::Response ret = {};
163- CURLcode res = CURLE_OK;
164-
165167 /* * initialize upload object */
166168 RestClient::Helpers::UploadObject up_obj;
167169 up_obj.data = data.c_str ();
@@ -179,8 +181,7 @@ RestClient::Response Connection::put(const std::string& url,
179181 curl_easy_setopt (this ->curlHandle , CURLOPT_INFILESIZE,
180182 static_cast <int64_t >(up_obj.length ));
181183
182- this ->performCurlRequest (url, ret);
183- return ret;
184+ return this ->performCurlRequest (url);
184185}
185186/* *
186187 * @brief HTTP DELETE method
@@ -189,18 +190,13 @@ RestClient::Response Connection::put(const std::string& url,
189190 *
190191 * @return response struct
191192 */
192- RestClient::Response Connection::del (const std::string& url) {
193- /* * create return struct */
194- RestClient::Response ret = {};
195- CURLcode res = CURLE_OK;
196-
193+ Response Connection::del (const std::string& url) {
197194 /* * we want HTTP DELETE */
198195 const char * http_delete = " DELETE" ;
199196
200197 /* * set HTTP DELETE METHOD */
201198 curl_easy_setopt (this ->curlHandle , CURLOPT_CUSTOMREQUEST, http_delete);
202199
203- this ->performCurlRequest (url, ret);
204- return ret;
200+ return this ->performCurlRequest (url);
205201}
206202
0 commit comments