Skip to content

Commit df8f0eb

Browse files
committed
Merge branch 'release/v0.1.2'
2 parents ca2a523 + 6fc943c commit df8f0eb

8 files changed

Lines changed: 63 additions & 18 deletions

File tree

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ This is a simple REST client for C++. It wraps libcurl for HTTP requests.
66
## Usage
77
I tried to keep usage close to the [ruby rest-client][]. So the basic usage is:
88

9-
restclient::method(url, content-type, params);
9+
RestClient::method(url, content-type, params);
1010

1111
Examples:
1212

1313
#include "restclient.h"
1414

15-
RestClient::get("http://url.com")
16-
RestClient::post("http://url.com/post", "text/json", "{"foo": "bla"}")
17-
RestClient::put("http://url.com/put", "text/json", "{"foo": "bla"}")
18-
RestClient::del("http://url.com/delete")
15+
RestClient::response r = RestClient::get("http://url.com")
16+
RestClient::response r = RestClient::post("http://url.com/post", "text/json", "{"foo": "bla"}")
17+
RestClient::response r = RestClient::put("http://url.com/put", "text/json", "{"foo": "bla"}")
18+
RestClient::response r = RestClient::del("http://url.com/delete")
1919

2020
The response is of type RestClient::response and has two attributes:
2121

@@ -28,6 +28,21 @@ The response is of type RestClient::response and has two attributes:
2828
- [gtest][] for development
2929

3030

31+
## Development
32+
- [Issues][]
33+
- [Tracker][]
34+
35+
## Contribute
36+
- Fork the project.
37+
- Make your feature addition or bug fix.
38+
- Add tests for it. This is important so I don't break it in a future version
39+
unintentionally.
40+
- Commit, do not mess with version
41+
- Send me a pull request. Bonus points for topic branches.
42+
43+
3144
[libcurl]: http://curl.haxx.se/libcurl/
3245
[ruby rest-client]: http://github.com/archiloque/rest-client
3346
[gtest]: http://code.google.com/p/googletest/
47+
[Issues]: https://github.com/mrtazz/restclient-cpp/issues
48+
[Tracker]: https://www.pivotaltracker.com/projects/255177/stories

include/meta.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define INCLUDE_META_H_
99

1010
// version string normally set from makefile
11-
#define VERSION "0.1.1"
11+
#define VERSION "0.1.2"
1212

1313
#endif // INCLUDE_META_H_
1414

include/restclient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <curl/curl.h>
1313
#include <string>
14+
#include <cstdlib>
1415
#include "meta.h"
1516

1617
class RestClient

source/restclient.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
========================*/
1010
#include "include/restclient.h"
1111

12+
#include <cstring>
1213
#include <string>
1314
#include <iostream>
1415

@@ -45,9 +46,9 @@ RestClient::response RestClient::get(const std::string& url)
4546
res = curl_easy_perform(curl);
4647
if (res != 0)
4748
{
48-
std::cerr << "Failed to query " << url << ":"
49-
<< std::endl << curl_easy_strerror(res) << std::endl << std::flush;
50-
exit(1);
49+
ret.body = "Failed to query.";
50+
ret.code = -1;
51+
return ret;
5152
}
5253
long http_code = 0;
5354
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
@@ -109,9 +110,9 @@ RestClient::response RestClient::post(const std::string& url,
109110
res = curl_easy_perform(curl);
110111
if (res != 0)
111112
{
112-
std::cerr << "Failed to query " << url << ":"
113-
<< std::endl << curl_easy_strerror(res) << std::endl << std::flush;
114-
exit(1);
113+
ret.body = "Failed to query.";
114+
ret.code = -1;
115+
return ret;
115116
}
116117
long http_code = 0;
117118
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
@@ -179,9 +180,9 @@ RestClient::response RestClient::put(const std::string& url,
179180
res = curl_easy_perform(curl);
180181
if (res != 0)
181182
{
182-
std::cerr << "Failed to query " << url << ":"
183-
<< std::endl << curl_easy_strerror(res) << std::endl << std::flush;
184-
exit(1);
183+
ret.body = "Failed to query.";
184+
ret.code = -1;
185+
return ret;
185186
}
186187
long http_code = 0;
187188
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
@@ -228,9 +229,9 @@ RestClient::response RestClient::del(const std::string& url)
228229
res = curl_easy_perform(curl);
229230
if (res != 0)
230231
{
231-
std::cerr << "Failed to query " << url << ":"
232-
<< std::endl << curl_easy_strerror(res) << std::endl << std::flush;
233-
exit(1);
232+
ret.body = "Failed to query.";
233+
ret.code = -1;
234+
return ret;
234235
}
235236
long http_code = 0;
236237
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

test/test_restclient_delete.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ TEST_F(RestClientDeleteTest, TestRestClientDeleteCode)
4242
RestClient::response res = RestClient::del(url);
4343
EXPECT_EQ(200, res.code);
4444
}
45+
// check for failure
46+
TEST_F(RestClientDeleteTest, TestRestClientFailureCode)
47+
{
48+
std::string u = "http://nonexistent";
49+
RestClient::response res = RestClient::del(u);
50+
EXPECT_EQ(-1, res.code);
51+
}

test/test_restclient_get.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ TEST_F(RestClientGetTest, TestRestClientGETCode)
4242
RestClient::response res = RestClient::get(url);
4343
EXPECT_EQ(200, res.code);
4444
}
45+
// check for failure
46+
TEST_F(RestClientGetTest, TestRestClientFailureCode)
47+
{
48+
std::string u = "http://nonexistent";
49+
RestClient::response res = RestClient::get(u);
50+
EXPECT_EQ(-1, res.code);
51+
}

test/test_restclient_post.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ TEST_F(RestClientPostTest, TestRestClientPOSTBody)
5656
ctype, data);
5757
EXPECT_EQ(data, res.body);
5858
}
59+
// check for failure
60+
TEST_F(RestClientPostTest, TestRestClientFailureCode)
61+
{
62+
std::string u = "http://nonexistent";
63+
RestClient::response res = RestClient::post(u, ctype, data);
64+
EXPECT_EQ(-1, res.code);
65+
}

test/test_restclient_put.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ TEST_F(RestClientPutTest, TestRestClientPUTBody)
5454
RestClient::response res = RestClient::put(url+"/body", ctype, data);
5555
EXPECT_EQ(data, res.body);
5656
}
57+
// check for failure
58+
TEST_F(RestClientPutTest, TestRestClientFailureCode)
59+
{
60+
std::string u = "http://nonexistent";
61+
RestClient::response res = RestClient::put(u, ctype, data);
62+
EXPECT_EQ(-1, res.code);
63+
}

0 commit comments

Comments
 (0)