Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
RESTClient Struct Reference

#include <RESTClient.h>

Overview

An HTTP REST client backed by URLSession.

RESTClient provides synchronous and asynchronous HTTP verb methods (HEAD, GET, OPTIONS, POST, PATCH, PUT, DELETE) dealing in Data * request and response bodies. JSON marshalling is the caller's responsibility, typically via JSONContext. Every verb accepts an optional headers array for attaching additional request headers.

Definition at line 62 of file RESTClient.h.

Inheritance diagram for RESTClient:
Object

Properties

Object object
 The superclass.
 
URLSession * session
 The URLSession backing this client.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _RESTClient (void)
 The RESTClient archetype.
 
int del (RESTClient *, const char *, const char **, Data **)
 Synchronously performs an HTTP DELETE request.
 
void delAsync (RESTClient *, const char *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP DELETE request.
 
int get (RESTClient *, const char *, const char **, Data **)
 Synchronously performs an HTTP GET request.
 
void getAsync (RESTClient *, const char *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP GET request.
 
int head (RESTClient *, const char *, const char **)
 Synchronously performs an HTTP HEAD request.
 
void headAsync (RESTClient *, const char *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP HEAD request.
 
RESTClient * init (RESTClient *)
 Initializes this RESTClient with the shared URLSession.
 
RESTClient * initWithSession (RESTClient *, URLSession *)
 Initializes this RESTClient with the specified URLSession.
 
int options (RESTClient *, const char *, const char **, Data **)
 Synchronously performs an HTTP OPTIONS request.
 
void optionsAsync (RESTClient *, const char *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP OPTIONS request.
 
int patch (RESTClient *, const char *, const Data *, const char **, Data **)
 Synchronously performs an HTTP PATCH request.
 
void patchAsync (RESTClient *, const char *, const Data *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP PATCH request.
 
int post (RESTClient *, const char *, const Data *, const char **, Data **)
 Synchronously performs an HTTP POST request.
 
void postAsync (RESTClient *, const char *, const Data *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP POST request.
 
int put (RESTClient *, const char *, const Data *, const char **, Data **)
 Synchronously performs an HTTP PUT request.
 
void putAsync (RESTClient *, const char *, const Data *, const char **, RESTClientCompletion, void *)
 Asynchronously performs an HTTP PUT request.
 
RESTClient * sharedInstance (void)
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

RESTClientInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ interface

RESTClientInterface* RESTClient::interface[0]
protected

The interface type.

Definition at line 73 of file RESTClient.h.

◆ object

Object RESTClient::object

The superclass.

Definition at line 67 of file RESTClient.h.

◆ session

URLSession* RESTClient::session

The URLSession backing this client.

Definition at line 78 of file RESTClient.h.

Method Details

◆ _RESTClient()

Class * _RESTClient ( void  )

The RESTClient archetype.

Returns
The RESTClient Class.

Definition at line 342 of file RESTClient.c.

342 {
343
344 static Class *clazz;
345 static Once once;
346
347 do_once(&once, {
348 clazz = _initialize(&(const ClassDef) {
349 .name = "RESTClient",
350 .superclass = _Object(),
351 .instanceSize = sizeof(RESTClient),
352 .interfaceSize = sizeof(RESTClientInterface),
353 .initialize = initialize,
354 .destroy = destroy,
355 });
356 });
357
358 return clazz;
359}
static void initialize(Class *clazz)
Definition Array.c:710
static void destroy(Class *clazz)
Definition Boole.c:102
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
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
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
An HTTP REST client backed by URLSession.
Definition RESTClient.h:62

◆ del()

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

Synchronously performs an HTTP DELETE request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 154 of file RESTClient.c.

154 {
155 return request(self, HTTP_DELETE, url, NULL, headers, data);
156}
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:57
@ HTTP_DELETE
Definition URLRequest.h:50

◆ delAsync()

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

Asynchronously performs an HTTP DELETE request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 162 of file RESTClient.c.

163 {
164 requestAsync(self, HTTP_DELETE, url, NULL, headers, completion, userData);
165}
static void requestAsync(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, RESTClientCompletion completion, void *userData)
Definition RESTClient.c:99

◆ get()

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

Synchronously performs an HTTP GET request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 171 of file RESTClient.c.

171 {
172 return request(self, HTTP_GET, url, NULL, headers, data);
173}
@ HTTP_GET
Definition URLRequest.h:46

◆ getAsync()

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

Asynchronously performs an HTTP GET request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 179 of file RESTClient.c.

180 {
181 requestAsync(self, HTTP_GET, url, NULL, headers, completion, userData);
182}

◆ head()

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

Synchronously performs an HTTP HEAD request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 188 of file RESTClient.c.

188 {
189 return request(self, HTTP_HEAD, url, NULL, headers, NULL);
190}
@ HTTP_HEAD
Definition URLRequest.h:44

◆ headAsync()

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

Asynchronously performs an HTTP HEAD request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 196 of file RESTClient.c.

197 {
198 requestAsync(self, HTTP_HEAD, url, NULL, headers, completion, userData);
199}

◆ init()

RESTClient * init ( RESTClient *  self)

Initializes this RESTClient with the shared URLSession.

Parameters
selfThe RESTClient.
Returns
The initialized RESTClient, or NULL on error.

Definition at line 205 of file RESTClient.c.

205 {
206 return $(self, initWithSession, $$(URLSession, sharedInstance));
207}
RESTClient * initWithSession(RESTClient *, URLSession *)
Initializes this RESTClient with the specified URLSession.
Definition RESTClient.c:213
RESTClient * sharedInstance(void)
Definition Log.c:212
A management context for loading resources via URLs.
Definition URLSession.h:57

◆ initWithSession()

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

Initializes this RESTClient with the specified URLSession.

Parameters
selfThe RESTClient.
sessionThe URLSession.
Returns
The initialized RESTClient, or NULL on error.

Definition at line 213 of file RESTClient.c.

213 {
214
215 self = (RESTClient *) super(Object, self, init);
216 if (self) {
217 assert(session);
218 self->session = retain(session);
219 }
220 return self;
221}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
URLSession * session
The URLSession backing this client.
Definition RESTClient.h:78
RESTClient * init(RESTClient *)
Initializes this RESTClient with the shared URLSession.
Definition RESTClient.c:205

◆ options()

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

Synchronously performs an HTTP OPTIONS request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 227 of file RESTClient.c.

227 {
228 return request(self, HTTP_OPTIONS, url, NULL, headers, data);
229}
@ HTTP_OPTIONS
Definition URLRequest.h:45

◆ optionsAsync()

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

Asynchronously performs an HTTP OPTIONS request.

Parameters
selfThe RESTClient.
urlThe URL string.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 235 of file RESTClient.c.

236 {
237 requestAsync(self, HTTP_OPTIONS, url, NULL, headers, completion, userData);
238}

◆ patch()

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

Synchronously performs an HTTP PATCH request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 244 of file RESTClient.c.

244 {
245 return request(self, HTTP_PATCH, url, body, headers, data);
246}
@ HTTP_PATCH
Definition URLRequest.h:49

◆ patchAsync()

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

Asynchronously performs an HTTP PATCH request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 252 of file RESTClient.c.

253 {
254 requestAsync(self, HTTP_PATCH, url, body, headers, completion, userData);
255}

◆ post()

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

Synchronously performs an HTTP POST request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 261 of file RESTClient.c.

261 {
262 return request(self, HTTP_POST, url, body, headers, data);
263}
@ HTTP_POST
Definition URLRequest.h:47

◆ postAsync()

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

Asynchronously performs an HTTP POST request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 269 of file RESTClient.c.

270 {
271 requestAsync(self, HTTP_POST, url, body, headers, completion, userData);
272}

◆ put()

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

Synchronously performs an HTTP PUT request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

Definition at line 278 of file RESTClient.c.

278 {
279 return request(self, HTTP_PUT, url, body, headers, data);
280}
@ HTTP_PUT
Definition URLRequest.h:48

◆ putAsync()

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

Asynchronously performs an HTTP PUT request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
headersA NULL-terminated array of alternating field/value C strings (e.g. { "X-Field", "value", NULL }) to add to the request, or NULL for none.
completionThe completion handler.
userDataUser data passed through to completion.

Definition at line 286 of file RESTClient.c.

287 {
288 requestAsync(self, HTTP_PUT, url, body, headers, completion, userData);
289}

◆ sharedInstance()

RESTClient * sharedInstance ( void  )
Returns
The shared RESTClient instance, backed by the shared URLSession.

Definition at line 212 of file Log.c.

212 {
213
214 static Once once;
215
216 do_once(&once, {
218 });
219
220 return _sharedInstance;
221}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
static Log * _sharedInstance
Definition Log.c:206
A Log4J-inspired log appender.
Definition Log.h:61

The documentation for this struct was generated from the following files: