Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
RESTClient.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <curl/curl.h>
#include "RESTClient.h"
#include "URL.h"
#include "URLRequest.h"
#include "URLSessionDataTask.h"

Go to the source code of this file.

Data Structures

struct  RESTClient_AsyncState
 

Macros

#define _Class   _RESTClient
 

Functions

Class * _RESTClient (void)
 
static void dealloc (Object *self)
 
static int del (RESTClient *self, const char *url, const char **headers, Data **data)
 
static void delAsync (RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
 
static void destroy (Class *clazz)
 
static int get (RESTClient *self, const char *url, const char **headers, Data **data)
 
static void getAsync (RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
 
static int head (RESTClient *self, const char *url, const char **headers)
 
static void headAsync (RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
 
static RESTClient * init (RESTClient *self)
 
static void initialize (Class *clazz)
 
static RESTClient * initWithSession (RESTClient *self, URLSession *session)
 
static int options (RESTClient *self, const char *url, const char **headers, Data **data)
 
static void optionsAsync (RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
 
static int patch (RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
 
static void patchAsync (RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
 
static int post (RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
 
static void postAsync (RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
 
static int put (RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
 
static void putAsync (RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
 
static int request (RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, Data **out_data)
 
static void requestAsync (RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
 
static void RESTClient_AsyncCompletion (URLSessionTask *task, bool success)
 
static RESTClient * sharedInstance (void)
 

Variables

static RESTClient * _sharedInstance
 

Macro Definition Documentation

◆ _Class

#define _Class   _RESTClient

Definition at line 34 of file RESTClient.c.

Function Documentation

◆ _RESTClient()

Class * _RESTClient ( void  )

Definition at line 361 of file RESTClient.c.

361 {
362
363 static Class *clazz;
364 static Once once;
365
366 do_once(&once, {
367 clazz = _initialize(&(const ClassDef) {
368 .name = "RESTClient",
369 .superclass = _Object(),
370 .instanceSize = sizeof(RESTClient),
371 .interfaceSize = sizeof(RESTClientInterface),
372 .initialize = initialize,
373 .destroy = destroy,
374 });
375 });
376
377 return clazz;
378}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
Class * _Object(void)
Definition Object.c:136
static void destroy(Class *clazz)
Definition RESTClient.c:329
static void initialize(Class *clazz)
Definition RESTClient.c:333
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
An HTTP REST client backed by URLSession.
Definition RESTClient.h:62

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 158 of file RESTClient.c.

158 {
159
160 RESTClient *this = (RESTClient *) self;
161
162 release(this->session);
163
164 super(Object, self, dealloc);
165}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition RESTClient.c:158
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ del()

static int del ( RESTClient *  self,
const char *  url,
const char **  headers,
Data **  data 
)
static

Definition at line 173 of file RESTClient.c.

173 {
174 return request(self, HTTP_DELETE, url, NULL, headers, data);
175}
static Data * data(void)
Definition Data.c:286
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, Data **out_data)
Definition RESTClient.c:62
@ HTTP_DELETE
Definition URLRequest.h:50

◆ delAsync()

static void delAsync ( RESTClient *  self,
const char *  url,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 181 of file RESTClient.c.

182 {
183 requestAsync(self, HTTP_DELETE, url, NULL, headers, completion, userData);
184}
static void requestAsync(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:111

◆ destroy()

static void destroy ( Class *  clazz)
static

Definition at line 329 of file RESTClient.c.

329 {
331}
static RESTClient * _sharedInstance
Definition RESTClient.c:310

◆ get()

static int get ( RESTClient *  self,
const char *  url,
const char **  headers,
Data **  data 
)
static

Definition at line 190 of file RESTClient.c.

190 {
191 return request(self, HTTP_GET, url, NULL, headers, data);
192}
@ HTTP_GET
Definition URLRequest.h:46

◆ getAsync()

static void getAsync ( RESTClient *  self,
const char *  url,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 198 of file RESTClient.c.

199 {
200 requestAsync(self, HTTP_GET, url, NULL, headers, completion, userData);
201}

◆ head()

static int head ( RESTClient *  self,
const char *  url,
const char **  headers 
)
static

Definition at line 207 of file RESTClient.c.

207 {
208 return request(self, HTTP_HEAD, url, NULL, headers, NULL);
209}
@ HTTP_HEAD
Definition URLRequest.h:44

◆ headAsync()

static void headAsync ( RESTClient *  self,
const char *  url,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 215 of file RESTClient.c.

216 {
217 requestAsync(self, HTTP_HEAD, url, NULL, headers, completion, userData);
218}

◆ init()

static RESTClient * init ( RESTClient *  self)
static

Definition at line 224 of file RESTClient.c.

224 {
225 return $(self, initWithSession, $$(URLSession, sharedInstance));
226}
static RESTClient * initWithSession(RESTClient *self, URLSession *session)
Definition RESTClient.c:232
static RESTClient * sharedInstance(void)
Definition RESTClient.c:316
A management context for loading resources via URLs.
Definition URLSession.h:59

◆ initialize()

static void initialize ( Class *  clazz)
static

Definition at line 333 of file RESTClient.c.

333 {
334
335 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
336
337 RESTClientInterface *rest = (RESTClientInterface *) clazz->interface;
338
339 rest->init = init;
341
342 rest->del = del;
343 rest->get = get;
344 rest->head = head;
345 rest->options = options;
346 rest->patch = patch;
347 rest->post = post;
348 rest->put = put;
349
350 rest->delAsync = delAsync;
351 rest->getAsync = getAsync;
352 rest->headAsync = headAsync;
353 rest->optionsAsync = optionsAsync;
354 rest->patchAsync = patchAsync;
355 rest->postAsync = postAsync;
356 rest->putAsync = putAsync;
357
358 rest->sharedInstance = sharedInstance;
359}
static int head(RESTClient *self, const char *url, const char **headers)
Definition RESTClient.c:207
static void getAsync(RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:198
static void putAsync(RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:305
static void optionsAsync(RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:254
static void delAsync(RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:181
static int del(RESTClient *self, const char *url, const char **headers, Data **data)
Definition RESTClient.c:173
static void patchAsync(RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:271
static int put(RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
Definition RESTClient.c:297
static RESTClient * init(RESTClient *self)
Definition RESTClient.c:224
static void postAsync(RESTClient *self, const char *url, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:288
static int post(RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
Definition RESTClient.c:280
static int get(RESTClient *self, const char *url, const char **headers, Data **data)
Definition RESTClient.c:190
static int patch(RESTClient *self, const char *url, const Data *body, const char **headers, Data **data)
Definition RESTClient.c:263
static void headAsync(RESTClient *self, const char *url, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:215
static int options(RESTClient *self, const char *url, const char **headers, Data **data)
Definition RESTClient.c:246
ident interface
The interface of the Class.
Definition Class.h:100
RESTClient * initWithSession(RESTClient *, URLSession *)
Initializes this RESTClient with the specified URLSession.
Definition RESTClient.c:232
int del(RESTClient *, const char *, const char **, Data **)
Synchronously performs an HTTP DELETE request.
Definition RESTClient.c:173

◆ initWithSession()

static RESTClient * initWithSession ( RESTClient *  self,
URLSession *  session 
)
static

Definition at line 232 of file RESTClient.c.

232 {
233
234 self = (RESTClient *) super(Object, self, init);
235 if (self) {
236 assert(session);
237 self->session = retain(session);
238 }
239 return self;
240}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
URLSession * session
The URLSession backing this client.
Definition RESTClient.h:78

◆ options()

static int options ( RESTClient *  self,
const char *  url,
const char **  headers,
Data **  data 
)
static

Definition at line 246 of file RESTClient.c.

246 {
247 return request(self, HTTP_OPTIONS, url, NULL, headers, data);
248}
@ HTTP_OPTIONS
Definition URLRequest.h:45

◆ optionsAsync()

static void optionsAsync ( RESTClient *  self,
const char *  url,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 254 of file RESTClient.c.

255 {
256 requestAsync(self, HTTP_OPTIONS, url, NULL, headers, completion, userData);
257}

◆ patch()

static int patch ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
Data **  data 
)
static

Definition at line 263 of file RESTClient.c.

263 {
264 return request(self, HTTP_PATCH, url, body, headers, data);
265}
@ HTTP_PATCH
Definition URLRequest.h:49

◆ patchAsync()

static void patchAsync ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 271 of file RESTClient.c.

272 {
273 requestAsync(self, HTTP_PATCH, url, body, headers, completion, userData);
274}

◆ post()

static int post ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
Data **  data 
)
static

Definition at line 280 of file RESTClient.c.

280 {
281 return request(self, HTTP_POST, url, body, headers, data);
282}
@ HTTP_POST
Definition URLRequest.h:47

◆ postAsync()

static void postAsync ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 288 of file RESTClient.c.

289 {
290 requestAsync(self, HTTP_POST, url, body, headers, completion, userData);
291}

◆ put()

static int put ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
Data **  data 
)
static

Definition at line 297 of file RESTClient.c.

297 {
298 return request(self, HTTP_PUT, url, body, headers, data);
299}
@ HTTP_PUT
Definition URLRequest.h:48

◆ putAsync()

static void putAsync ( RESTClient *  self,
const char *  url,
const Data *  body,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 305 of file RESTClient.c.

306 {
307 requestAsync(self, HTTP_PUT, url, body, headers, completion, userData);
308}

◆ request()

static int request ( RESTClient *  self,
HTTPMethod  method,
const char *  url_string,
const Data *  body,
const char **  headers,
Data **  out_data 
)
static

Definition at line 62 of file RESTClient.c.

63 {
64
65 if (out_data) {
66 *out_data = NULL;
67 }
68
70
71 if (method == HTTP_GET && !headers) {
72 URL *url = $(alloc(URL), initWithCharacters, url_string);
73 task = $(self->session, dataTaskWithURL, url, NULL);
74 release(url);
75 } else {
76 URL *url = $(alloc(URL), initWithCharacters, url_string);
78 release(url);
79 request->httpMethod = method;
80 if (body) {
81 request->httpBody = retain((Data *) body);
82 $(request, setValueForHTTPHeaderField, "application/json", "Content-Type");
83 }
84 if (headers) {
85 for (size_t i = 0; headers[i] && headers[i + 1]; i += 2) {
86 $(request, setValueForHTTPHeaderField, headers[i + 1], headers[i]);
87 }
88 }
89 task = $(self->session, dataTaskWithRequest, request, NULL);
91 }
92
93 if (task == NULL) {
94 return 0;
95 }
96
97 $((URLSessionTask *) task, execute);
98
99 const URLSessionTask *t = (URLSessionTask *) task;
100 const bool success = t->state == URLSESSIONTASK_COMPLETED && t->result == CURLE_OK;
101
102 const int status = success ? t->response->httpStatusCode : 0;
103 if (success && out_data && task->data) {
104 *out_data = retain(task->data);
105 }
106
107 release(task);
108 return status;
109}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
static String * initWithCharacters(String *self, const char *chars)
Definition String.c:633
static ThreadPoolWork * execute(ThreadPool *self, ThreadFunction function, ident data)
Definition ThreadPool.c:192
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition URLRequest.c:150
static void setValueForHTTPHeaderField(URLRequest *self, const char *value, const char *field)
Definition URLRequest.c:169
static URLSessionDataTask * dataTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
Definition URLSession.c:85
static URLSessionDataTask * dataTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
Definition URLSession.c:129
@ URLSESSIONTASK_COMPLETED
Data buffers.
Definition Data.h:50
Uniform Resource Locators (RFC 3986).
Definition URL.h:44
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58
int httpStatusCode
The HTTP response status code.
Definition URLResponse.h:63
Use data tasks to send and receive Data in-memory.
Data * data
The data received.
URL session tasks are handles to pending URL operations.
struct URLResponse * response
The response.
int result
The libcurl result code (CURLcode) of the transfer, 0 on success.
URLSessionTaskState state
The state.

◆ requestAsync()

static void requestAsync ( RESTClient *  self,
HTTPMethod  method,
const char *  url_string,
const Data *  body,
const char **  headers,
RESTClientCompletion  completion,
void *  userData 
)
static

Definition at line 111 of file RESTClient.c.

112 {
113
114 URLSessionDataTask *task;
115
116 if (method == HTTP_GET && !headers) {
117 URL *url = $(alloc(URL), initWithCharacters, url_string);
119 release(url);
120 } else {
121 URL *url = $(alloc(URL), initWithCharacters, url_string);
123 release(url);
124 request->httpMethod = method;
125 if (body) {
126 request->httpBody = retain((Data *) body);
127 $(request, setValueForHTTPHeaderField, "application/json", "Content-Type");
128 }
129 if (headers) {
130 for (size_t i = 0; headers[i] && headers[i + 1]; i += 2) {
131 $(request, setValueForHTTPHeaderField, headers[i + 1], headers[i]);
132 }
133 }
136 }
137
138 if (task == NULL) {
139 if (completion) {
140 completion(0, NULL, userData);
141 }
142 return;
143 }
144
145 RESTClient_AsyncState *state = calloc(1, sizeof(RESTClient_AsyncState));
146 state->completion = completion;
147 state->userData = userData;
148 task->urlSessionTask.data = state;
149
150 $((URLSessionTask *) task, resume);
151}
static void resume(OperationQueue *self)
static void RESTClient_AsyncCompletion(URLSessionTask *task, bool success)
Definition RESTClient.c:43
RESTClientCompletion completion
Definition RESTClient.c:39
URLSessionTask urlSessionTask
The superclass.
ident data
User data.

◆ RESTClient_AsyncCompletion()

static void RESTClient_AsyncCompletion ( URLSessionTask *  task,
bool  success 
)
static

Definition at line 43 of file RESTClient.c.

43 {
44
45 RESTClient_AsyncState *state = task->data;
46 URLSessionDataTask *dataTask = (URLSessionDataTask *) task;
47
48 if (state->completion) {
49 if (success) {
50 state->completion(task->response->httpStatusCode, dataTask->data, state->userData);
51 } else {
52 state->completion(0, NULL, state->userData);
53 }
54 }
55
56 free(state);
57 release(task);
58}

◆ sharedInstance()

static RESTClient * sharedInstance ( void  )
static

Definition at line 316 of file RESTClient.c.

316 {
317
318 static Once once;
319
320 do_once(&once, {
322 });
323
324 return _sharedInstance;
325}

Variable Documentation

◆ _sharedInstance

RESTClient* _sharedInstance
static

Definition at line 310 of file RESTClient.c.