Skip to content

Commit 2214d6a

Browse files
committed
make refactor compile
1 parent 63daede commit 2214d6a

5 files changed

Lines changed: 52 additions & 62 deletions

File tree

include/restclient-cpp/connection.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <map>
1515
#include <cstdlib>
1616

17+
#include "restclient-cpp/restclient.h"
1718
#include "restclient-cpp/version.h"
1819

1920
/**
@@ -26,15 +27,6 @@ namespace RestClient {
2627
*/
2728
class Connection {
2829
public:
29-
/**
30-
* @brief enum to hold identifiers for HTTP verbs
31-
*/
32-
enum HttpVerb {
33-
GET,
34-
POST,
35-
PUT,
36-
DELETE
37-
};
3830

3931
/**
4032
* @struct Info
@@ -43,7 +35,7 @@ class Connection {
4335
*/
4436
typedef struct {
4537
std::string base_url;
46-
RestClients::HeaderFields headers;
38+
RestClient::HeaderFields headers;
4739
int timeout;
4840
struct {
4941
std::string username;
@@ -112,10 +104,10 @@ class Connection {
112104
const std::string& data);
113105
RestClient::Response put(const std::string& uri,
114106
const std::string& data);
115-
RestCLient::Response del(const std::string& uri);
107+
RestClient::Response del(const std::string& uri);
116108

117109
private:
118-
CURL* curlHandle = NULL;
110+
CURL* curlHandle;
119111
std::string baseUrl;
120112
RestClient::HeaderFields headerFields;
121113
int timeout;
@@ -125,8 +117,7 @@ class Connection {
125117
} basicAuth;
126118
std::string customUserAgent;
127119
Info infoStruct;
128-
int performCurlRequest(const std::string& uri,
129-
RestClient::Response& ret);
120+
RestClient::Response performCurlRequest(const std::string& uri);
130121
};
131122
}; // namespace RestClient
132123

include/restclient-cpp/helpers.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ namespace Helpers {
3939
} UploadObject;
4040

4141
// writedata callback function
42-
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
42+
size_t write_callback(void *ptr, size_t size, size_t nmemb,
4343
void *userdata);
4444

4545
// header callback function
46-
static size_t header_callback(void *ptr, size_t size, size_t nmemb,
46+
size_t header_callback(void *ptr, size_t size, size_t nmemb,
4747
void *userdata);
4848
// read callback function
49-
static size_t read_callback(void *ptr, size_t size, size_t nmemb,
49+
size_t read_callback(void *ptr, size_t size, size_t nmemb,
5050
void *userdata);
5151

5252
// trim from start
53-
static inline std::string &ltrim(const std::string &s) {
53+
static inline std::string &ltrim(std::string &s) {
5454
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
5555
std::not1(std::ptr_fun<int, int>(std::isspace))));
5656
return s;
5757
}
5858

5959
// trim from end
60-
static inline std::string &rtrim(const std::string &s) {
60+
static inline std::string &rtrim(std::string &s) {
6161
s.erase(std::find_if(s.rbegin(), s.rend(),
6262
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
6363
return s;
6464
}
6565

6666
// trim from both ends
67-
static inline std::string &trim(const std::string &s) {
67+
static inline std::string &trim(std::string &s) {
6868
return ltrim(rtrim(s));
6969
}
7070
}; // namespace Helpers

source/connection.cc

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
using 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

3231
Connection::~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
*/
119134
Response 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
*/
136146
Response 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

source/helpers.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ size_t RestClient::Helpers::header_callback(void *data, size_t size,
7272
size_t RestClient::Helpers::read_callback(void *data, size_t size,
7373
size_t nmemb, void *userdata) {
7474
/** get upload struct */
75-
RestClient::upload_object* u;
76-
u = reinterpret_cast<RestClient::upload_object*>(userdata);
75+
RestClient::Helpers::UploadObject* u;
76+
u = reinterpret_cast<RestClient::Helpers::UploadObject*>(userdata);
7777
/** set correct sizes */
7878
size_t curl_size = size * nmemb;
7979
size_t copy_size = (u->length < curl_size) ? u->length : curl_size;

source/restclient.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "restclient-cpp/restclient.h"
1515
#include "restclient-cpp/version.h"
1616

17+
#include "restclient-cpp/connection.h"
1718

1819
/**
1920
* @brief HTTP GET method
@@ -22,8 +23,8 @@
2223
*
2324
* @return response struct
2425
*/
25-
RestClient::response RestClient::get(const std::string& url) {
26-
Connection *conn = new Connection("");
26+
RestClient::Response RestClient::get(const std::string& url) {
27+
RestClient::Connection *conn = new RestClient::Connection("");
2728
return conn->get(url);
2829
}
2930

@@ -36,11 +37,12 @@ RestClient::response RestClient::get(const std::string& url) {
3637
*
3738
* @return response struct
3839
*/
39-
RestClient::response RestClient::post(const std::string& url,
40+
RestClient::Response RestClient::post(const std::string& url,
4041
const std::string& ctype,
4142
const std::string& data) {
42-
Connection *conn = new Connection("");
43-
return conn->post(url, ctype, data);
43+
RestClient::Connection *conn = new RestClient::Connection("");
44+
conn->AppendHeader("Content-Type", ctype);
45+
return conn->post(url, data);
4446
}
4547

4648
/**
@@ -52,11 +54,12 @@ RestClient::response RestClient::post(const std::string& url,
5254
*
5355
* @return response struct
5456
*/
55-
RestClient::response RestClient::put(const std::string& url,
57+
RestClient::Response RestClient::put(const std::string& url,
5658
const std::string& ctype,
5759
const std::string& data) {
58-
Connection *conn = new Connection("");
59-
return conn->put(url, ctype, data);
60+
RestClient::Connection *conn = new RestClient::Connection("");
61+
conn->AppendHeader("Content-Type", ctype);
62+
return conn->put(url, data);
6063
}
6164

6265
/**
@@ -66,8 +69,8 @@ RestClient::response RestClient::put(const std::string& url,
6669
*
6770
* @return response struct
6871
*/
69-
RestClient::response RestClient::del(const std::string& url) {
70-
Connection *conn = new Connection("");
72+
RestClient::Response RestClient::del(const std::string& url) {
73+
RestClient::Connection *conn = new RestClient::Connection("");
7174
return conn->del(url);
7275
}
7376

0 commit comments

Comments
 (0)