Objectively
Ultra-lightweight object oriented framework for GNU 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.

Definition at line 61 of file RESTClient.h.

Inheritance diagram for RESTClient:
Object

Properties

Object object
 The superclass.
 
URLSessionsession
 The URLSession backing this client.
 
- Properties inherited from Object
Classclazz
 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 get (RESTClient *, const char *, Data **)
 Synchronously performs an HTTP GET request.
 
void getAsync (RESTClient *, const char *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP GET request.
 
int head (RESTClient *, const char *)
 Synchronously performs an HTTP HEAD request.
 
void headAsync (RESTClient *, const char *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP HEAD request.
 
int httpDelete (RESTClient *, const char *, Data **)
 Synchronously performs an HTTP DELETE request.
 
void httpDeleteAsync (RESTClient *, const char *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP DELETE request.
 
RESTClientinit (RESTClient *)
 Initializes this RESTClient with the shared URLSession.
 
RESTClientinitWithSession (RESTClient *, URLSession *)
 Initializes this RESTClient with the specified URLSession.
 
int options (RESTClient *, const char *, Data **)
 Synchronously performs an HTTP OPTIONS request.
 
void optionsAsync (RESTClient *, const char *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP OPTIONS request.
 
int patch (RESTClient *, const char *, const Data *, Data **)
 Synchronously performs an HTTP PATCH request.
 
void patchAsync (RESTClient *, const char *, const Data *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP PATCH request.
 
int post (RESTClient *, const char *, const Data *, Data **)
 Synchronously performs an HTTP POST request.
 
void postAsync (RESTClient *, const char *, const Data *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP POST request.
 
int put (RESTClient *, const char *, const Data *, Data **)
 Synchronously performs an HTTP PUT request.
 
void putAsync (RESTClient *, const char *, const Data *, RESTClientCompletion, void *)
 Asynchronously performs an HTTP PUT request.
 
RESTClientsharedInstance (void)
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (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
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ interface

RESTClientInterface* RESTClient::interface
protected

The interface.

Definition at line 72 of file RESTClient.h.

◆ object

Object RESTClient::object

The superclass.

Definition at line 66 of file RESTClient.h.

◆ session

URLSession* RESTClient::session

The URLSession backing this client.

Definition at line 77 of file RESTClient.h.

Method Details

◆ _RESTClient()

Class * _RESTClient ( void  )

The RESTClient archetype.

Returns
The RESTClient Class.

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}
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:86
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
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:61
RESTClientInterface * interface
The interface.
Definition RESTClient.h:72

◆ get()

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

Synchronously performs an HTTP GET request.

Parameters
selfThe RESTClient.
urlThe URL string.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP GET request.

Parameters
selfThe RESTClient.
urlThe URL string.
completionThe completion handler.
user_dataUser data passed through to completion.

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()

int head ( RESTClient self,
const char *  url 
)

Synchronously performs an HTTP HEAD request.

Parameters
selfThe RESTClient.
urlThe URL string.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP HEAD request.

Parameters
selfThe RESTClient.
urlThe URL string.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 169 of file RESTClient.c.

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

◆ httpDelete()

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

Synchronously performs an HTTP DELETE request.

Parameters
selfThe RESTClient.
urlThe URL string.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP DELETE request.

Parameters
selfThe RESTClient.
urlThe URL string.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 186 of file RESTClient.c.

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

◆ 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 195 of file RESTClient.c.

195 {
196 return $(self, initWithSession, $$(URLSession, sharedInstance));
197}
RESTClient * initWithSession(RESTClient *, URLSession *)
Initializes this RESTClient with the specified URLSession.
Definition RESTClient.c:203
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 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
#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:77
RESTClient * init(RESTClient *)
Initializes this RESTClient with the shared URLSession.
Definition RESTClient.c:195

◆ options()

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

Synchronously performs an HTTP OPTIONS request.

Parameters
selfThe RESTClient.
urlThe URL string.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP OPTIONS request.

Parameters
selfThe RESTClient.
urlThe URL string.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 225 of file RESTClient.c.

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

◆ patch()

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

Synchronously performs an HTTP PATCH request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP PATCH request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 242 of file RESTClient.c.

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

◆ post()

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

Synchronously performs an HTTP POST request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP POST request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 259 of file RESTClient.c.

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

◆ put()

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

Synchronously performs an HTTP PUT request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
dataOptionally receives the retained response body. Caller must release.
Returns
The HTTP response status code, or 0 on connection failure.

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()

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

Asynchronously performs an HTTP PUT request.

Parameters
selfThe RESTClient.
urlThe URL string.
bodyThe request body, or NULL.
completionThe completion handler.
user_dataUser data passed through to completion.

Definition at line 276 of file RESTClient.c.

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

◆ 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:176
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: