Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
RESTClient.c File Reference
#include <assert.h>
#include <stdlib.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 void destroy (Class *clazz)
 
static int get (RESTClient *self, const char *url, Data **data)
 
static void getAsync (RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
 
static int head (RESTClient *self, const char *url)
 
static void headAsync (RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
 
static int httpDelete (RESTClient *self, const char *url, Data **data)
 
static void httpDeleteAsync (RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
 
static RESTClientinit (RESTClient *self)
 
static void initialize (Class *clazz)
 
static RESTClientinitWithSession (RESTClient *self, URLSession *session)
 
static int options (RESTClient *self, const char *url, Data **data)
 
static void optionsAsync (RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
 
static int patch (RESTClient *self, const char *url, const Data *body, Data **data)
 
static void patchAsync (RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
 
static int post (RESTClient *self, const char *url, const Data *body, Data **data)
 
static void postAsync (RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
 
static int put (RESTClient *self, const char *url, const Data *body, Data **data)
 
static void putAsync (RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
 
static int request (RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
 
static void requestAsync (RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, RESTClientCompletion completion, void *user_data)
 
static void RESTClient_AsyncCompletion (URLSessionTask *task, bool success)
 
static RESTClientsharedInstance (void)
 

Variables

static RESTClient_sharedInstance
 

Macro Definition Documentation

◆ _Class

#define _Class   _RESTClient

Definition at line 32 of file RESTClient.c.

Function Documentation

◆ _RESTClient()

Class * _RESTClient ( void  )

Definition at line 332 of file RESTClient.c.

332 {
333
334 static Class *clazz;
335 static Once once;
336
337 do_once(&once, {
338 clazz = _initialize(&(const ClassDef) {
339 .name = "RESTClient",
340 .superclass = _Object(),
341 .instanceSize = sizeof(RESTClient),
342 .interfaceOffset = offsetof(RESTClient, interface),
343 .interfaceSize = sizeof(RESTClientInterface),
345 .destroy = destroy,
346 });
347 });
348
349 return clazz;
350}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void destroy(Class *clazz)
Definition RESTClient.c:300
static void initialize(Class *clazz)
Definition RESTClient.c:304
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:95
An HTTP REST client backed by URLSession.
Definition RESTClient.h:61

◆ dealloc()

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

Definition at line 129 of file RESTClient.c.

129 {
130
131 RESTClient *this = (RESTClient *) self;
132
133 release(this->session);
134
135 super(Object, self, dealloc);
136}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition RESTClient.c:129
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ destroy()

static void destroy ( Class clazz)
static

Definition at line 300 of file RESTClient.c.

300 {
302}
static RESTClient * _sharedInstance
Definition RESTClient.c:281

◆ get()

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

Definition at line 144 of file RESTClient.c.

144 {
145 return request(self, HTTP_GET, url, NULL, data);
146}
static Data * data(void)
Definition Data.c:286
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
Definition RESTClient.c:57
@ HTTP_GET
Definition URLRequest.h:46

◆ getAsync()

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

Definition at line 152 of file RESTClient.c.

153 {
154 requestAsync(self, HTTP_GET, url, NULL, completion, user_data);
155}
static void requestAsync(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:94

◆ head()

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

Definition at line 161 of file RESTClient.c.

161 {
162 return request(self, HTTP_HEAD, url, NULL, NULL);
163}
@ HTTP_HEAD
Definition URLRequest.h:44

◆ headAsync()

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

Definition at line 169 of file RESTClient.c.

170 {
171 requestAsync(self, HTTP_HEAD, url, NULL, completion, user_data);
172}

◆ httpDelete()

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

Definition at line 178 of file RESTClient.c.

178 {
179 return request(self, HTTP_DELETE, url, NULL, data);
180}
@ HTTP_DELETE
Definition URLRequest.h:50

◆ httpDeleteAsync()

static void httpDeleteAsync ( RESTClient self,
const char *  url,
RESTClientCompletion  completion,
void *  user_data 
)
static

Definition at line 186 of file RESTClient.c.

187 {
188 requestAsync(self, HTTP_DELETE, url, NULL, completion, user_data);
189}

◆ init()

static RESTClient * init ( RESTClient self)
static

Definition at line 195 of file RESTClient.c.

195 {
196 return $(self, initWithSession, $$(URLSession, sharedInstance));
197}
static RESTClient * initWithSession(RESTClient *self, URLSession *session)
Definition RESTClient.c:203
static RESTClient * sharedInstance(void)
Definition RESTClient.c:287
A management context for loading resources via URLs.
Definition URLSession.h:57

◆ initialize()

static void initialize ( Class clazz)
static

Definition at line 304 of file RESTClient.c.

304 {
305
306 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
307
308 RESTClientInterface *rest = (RESTClientInterface *) clazz->interface;
309
310 rest->init = init;
312
313 rest->httpDelete = httpDelete;
314 rest->head = head;
315 rest->get = get;
316 rest->patch = patch;
317 rest->post = post;
318 rest->put = put;
319 rest->options = options;
320
321 rest->httpDeleteAsync = httpDeleteAsync;
322 rest->headAsync = headAsync;
323 rest->getAsync = getAsync;
324 rest->patchAsync = patchAsync;
325 rest->postAsync = postAsync;
326 rest->putAsync = putAsync;
327 rest->optionsAsync = optionsAsync;
328
329 rest->sharedInstance = sharedInstance;
330}
static int put(RESTClient *self, const char *url, const Data *body, Data **data)
Definition RESTClient.c:268
static void headAsync(RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:169
static void putAsync(RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:276
static int get(RESTClient *self, const char *url, Data **data)
Definition RESTClient.c:144
static int patch(RESTClient *self, const char *url, const Data *body, Data **data)
Definition RESTClient.c:234
static void patchAsync(RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:242
static void postAsync(RESTClient *self, const char *url, const Data *body, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:259
static int httpDelete(RESTClient *self, const char *url, Data **data)
Definition RESTClient.c:178
static int options(RESTClient *self, const char *url, Data **data)
Definition RESTClient.c:217
static RESTClient * init(RESTClient *self)
Definition RESTClient.c:195
static void getAsync(RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:152
static int head(RESTClient *self, const char *url)
Definition RESTClient.c:161
static void httpDeleteAsync(RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:186
static void optionsAsync(RESTClient *self, const char *url, RESTClientCompletion completion, void *user_data)
Definition RESTClient.c:225
static int post(RESTClient *self, const char *url, const Data *body, Data **data)
Definition RESTClient.c:251
ident interface
The interface of the Class.
Definition Class.h:105
RESTClient * initWithSession(RESTClient *, URLSession *)
Initializes this RESTClient with the specified URLSession.
Definition RESTClient.c:203
int httpDelete(RESTClient *, const char *, Data **)
Synchronously performs an HTTP DELETE request.
Definition RESTClient.c:178

◆ initWithSession()

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

Definition at line 203 of file RESTClient.c.

203 {
204
205 self = (RESTClient *) super(Object, self, init);
206 if (self) {
207 assert(session);
208 self->session = retain(session);
209 }
210 return self;
211}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
URLSession * session
The URLSession backing this client.
Definition RESTClient.h:77

◆ options()

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

Definition at line 217 of file RESTClient.c.

217 {
218 return request(self, HTTP_OPTIONS, url, NULL, data);
219}
@ HTTP_OPTIONS
Definition URLRequest.h:45

◆ optionsAsync()

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

Definition at line 225 of file RESTClient.c.

226 {
227 requestAsync(self, HTTP_OPTIONS, url, NULL, completion, user_data);
228}

◆ patch()

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

Definition at line 234 of file RESTClient.c.

234 {
235 return request(self, HTTP_PATCH, url, body, data);
236}
@ HTTP_PATCH
Definition URLRequest.h:49

◆ patchAsync()

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

Definition at line 242 of file RESTClient.c.

243 {
244 requestAsync(self, HTTP_PATCH, url, body, completion, user_data);
245}

◆ post()

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

Definition at line 251 of file RESTClient.c.

251 {
252 return request(self, HTTP_POST, url, body, data);
253}
@ HTTP_POST
Definition URLRequest.h:47

◆ postAsync()

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

Definition at line 259 of file RESTClient.c.

260 {
261 requestAsync(self, HTTP_POST, url, body, completion, user_data);
262}

◆ put()

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

Definition at line 268 of file RESTClient.c.

268 {
269 return request(self, HTTP_PUT, url, body, data);
270}
@ HTTP_PUT
Definition URLRequest.h:48

◆ putAsync()

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

Definition at line 276 of file RESTClient.c.

277 {
278 requestAsync(self, HTTP_PUT, url, body, completion, user_data);
279}

◆ request()

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

Definition at line 57 of file RESTClient.c.

58 {
59
60 if (out_data) {
61 *out_data = NULL;
62 }
63
65
66 if (method == HTTP_GET) {
67 URL *url = $(alloc(URL), initWithCharacters, url_string);
68 task = $(self->session, dataTaskWithURL, url, NULL);
69 release(url);
70 } else {
71 URL *url = $(alloc(URL), initWithCharacters, url_string);
73 release(url);
74 request->httpMethod = method;
75 if (body) {
76 request->httpBody = retain((Data *) body);
77 $(request, setValueForHTTPHeaderField, "application/json", "Content-Type");
78 }
79 task = $(self->session, dataTaskWithRequest, request, NULL);
81 }
82
83 $((URLSessionTask *) task, execute);
84
85 const int status = task->urlSessionTask.response->httpStatusCode;
86 if (out_data && task->data) {
87 *out_data = retain(task->data);
88 }
89
90 release(task);
91 return status;
92}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static String * initWithCharacters(String *self, const char *chars)
Definition String.c:638
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:76
static URLSessionDataTask * dataTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
Definition URLSession.c:103
static void execute(URLSessionTask *self)
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.
URLSessionTask urlSessionTask
The superclass.
URL session tasks are handles to pending URL operations.
struct URLResponse * response
The response.

◆ requestAsync()

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

Definition at line 94 of file RESTClient.c.

95 {
96
98
99 if (method == HTTP_GET) {
100 URL *url = $(alloc(URL), initWithCharacters, url_string);
102 release(url);
103 } else {
104 URL *url = $(alloc(URL), initWithCharacters, url_string);
106 release(url);
107 request->httpMethod = method;
108 if (body) {
109 request->httpBody = retain((Data *) body);
110 $(request, setValueForHTTPHeaderField, "application/json", "Content-Type");
111 }
114 }
115
116 RESTClient_AsyncState *state = calloc(1, sizeof(RESTClient_AsyncState));
117 state->completion = completion;
118 state->user_data = user_data;
119 task->urlSessionTask.data = state;
120
121 $((URLSessionTask *) task, resume);
122}
static void RESTClient_AsyncCompletion(URLSessionTask *task, bool success)
Definition RESTClient.c:41
static void resume(URLSessionTask *self)
RESTClientCompletion completion
Definition RESTClient.c:37
ident data
User data.

◆ RESTClient_AsyncCompletion()

static void RESTClient_AsyncCompletion ( URLSessionTask task,
bool  success 
)
static

Definition at line 41 of file RESTClient.c.

41 {
42
43 RESTClient_AsyncState *state = task->data;
44 URLSessionDataTask *dataTask = (URLSessionDataTask *) task;
45 const int status = task->response ? task->response->httpStatusCode : 0;
46
47 if (state->completion) {
48 state->completion(status, dataTask->data, state->user_data);
49 }
50
51 free(state);
52 release(task);
53}

◆ sharedInstance()

static RESTClient * sharedInstance ( void  )
static

Definition at line 287 of file RESTClient.c.

287 {
288
289 static Once once;
290
291 do_once(&once, {
293 });
294
295 return _sharedInstance;
296}

Variable Documentation

◆ _sharedInstance

RESTClient* _sharedInstance
static

Definition at line 281 of file RESTClient.c.