Skip to content

Commit bec64f4

Browse files
committed
Merge branch 'release-0.1.0'
2 parents 3cb1eb4 + eefb3cb commit bec64f4

File tree

12 files changed

+674
-0
lines changed

12 files changed

+674
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/*
2+
*.o

Makefile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# Makefile for restclient-cpp
3+
#
4+
5+
# set compiler and linker
6+
CC = g++
7+
LD = g++
8+
9+
# logfile for HTTP test server
10+
LOGFILE = test_server.log
11+
12+
# binaries and folders
13+
BINDIR = bin
14+
TEST = $(BINDIR)/test
15+
16+
# set library and include paths
17+
INCLUDE = -I. -I/usr/local/include
18+
TESTLIBS = -lgtest -lcurl
19+
LIBS = -lcurl
20+
21+
# set compiler and linker flags
22+
CCFLAGS = -O3 -W -Wall
23+
LDFLAGS = -W -Wall -L/usr/local/lib
24+
25+
# source files
26+
SRCS = source/restclient.cpp
27+
SRCS += $(wildcard test/test*.cpp)
28+
29+
# dependencies
30+
# object files
31+
OBJS = $(SRCS:.cpp=.o)
32+
33+
# linking rule
34+
$(TEST): $(OBJS) $(BINDIR)
35+
$(LD) $(LDFLAGS) $(OBJS) -o $(TEST) $(TESTLIBS)
36+
37+
# compile rule
38+
.cpp.o:
39+
$(CC) $(CCFLAGS) $(INCLUDE) -c $< -o $@
40+
41+
42+
$(BINDIR):
43+
@mkdir -p $(BINDIR)
44+
45+
# tasks
46+
.PHONY: clean all
47+
48+
clean:
49+
@rm -rf test/*.o
50+
@rm -rf source/*.o
51+
@rm -rf bin
52+
53+
all: $(TEST)
54+
@echo Running tests...
55+
@./bin/test

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# REST client for C++
2+
3+
## About
4+
This is a simple REST client for C++. It wraps libcurl for HTTP requests
5+
and uses jsoncpp for parsing JSON responses.
6+
7+
## Usage
8+
I tried to keep usage close to the [ruby rest-client][]. So the basic usage is:
9+
10+
restclient::method(url, content-type, params);
11+
12+
Examples:
13+
14+
#include "restclient.h"
15+
16+
RestClient::get("http://url.com")
17+
RestClient::post("http://url.com/post", "text/json", "{"foo": "bla"}")
18+
RestClient::put("http://url.com/put", "text/json", "{"foo": "bla"}")
19+
RestClient::del("http://url.com/delete")
20+
21+
## Dependencies
22+
- [libcurl][]
23+
- [jsoncpp][]
24+
25+
26+
[libcurl]: http://curl.haxx.se/libcurl/
27+
[jsoncpp]: http://jsoncpp.sourceforge.net/
28+
[ruby rest-client]: http://github.com/archiloque/rest-client

include/meta.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @file meta.h
3+
* @brief header file for meta information
4+
* @author Daniel Schauenberg <d@unwiredcouch.com>
5+
*/
6+
7+
#ifndef INCLUDE_META_H_
8+
#define INCLUDE_META_H_
9+
10+
// version string normally set from makefile
11+
#define VERSION "0.1.0"
12+
13+
#endif // INCLUDE_META_H_
14+

include/restclient.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @file restclient.h
3+
* @brief libcurl wrapper for REST calls
4+
* @author Daniel Schauenberg <d@unwiredcouch.com>
5+
* @version
6+
* @date 2010-10-11
7+
*/
8+
9+
#ifndef INCLUDE_RESTCLIENT_H_
10+
#define INCLUDE_RESTCLIENT_H_
11+
12+
#include <curl/curl.h>
13+
#include <string>
14+
#include "include/meta.h"
15+
16+
class RestClient
17+
{
18+
public:
19+
/**
20+
* public data definitions
21+
*/
22+
/** response struct for queries */
23+
typedef struct
24+
{
25+
int code;
26+
std::string body;
27+
} response;
28+
/** struct used for uploading data */
29+
typedef struct
30+
{
31+
const char* data;
32+
size_t length;
33+
} upload_object;
34+
35+
/** public methods */
36+
// HTTP GET
37+
static response get(const std::string& url);
38+
// HTTP POST
39+
static response post(const std::string& url, const std::string& ctype,
40+
const std::string& data);
41+
// HTTP PUT
42+
static response put(const std::string& url, const std::string& ctype,
43+
const std::string& data);
44+
// HTTP DELETE
45+
static response del(const std::string& url);
46+
47+
private:
48+
// writedata callback function
49+
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
50+
void *userdata);
51+
// read callback function
52+
static size_t read_callback(void *ptr, size_t size, size_t nmemb,
53+
void *userdata);
54+
static const char* user_agent;
55+
};
56+
57+
#endif // INCLUDE_RESTCLIENT_H_

0 commit comments

Comments
 (0)