NSRRequest Class Reference
Inherits from | NSObject |
Conforms to | NSCoding |
Declared in | NSRRequest.h |
Overview
NSRRequest is a class used internally by NSRRemoteObject to make remoteX
requests, but can also be used directly to construct custom resource paths, etc.
The methods in this class are set up so that making requests is concise and visually logical:
//GET to /posts/1
id response = [[[NSRRequest GET] routeToObject:post] sendSynchronous:&e];
//PATCH to /posts/1 with post as body
NSRRequest *request = [NSRRequest PATCH];
[request routeToObject:post];
[request setBodyToObject:post];
id response = [request sendSynchronous:&e];
Factory methods also exist (used by NSRRemoteObject) if you’d like to make only an incremental change to an existing request:
NSRRequest *request = [NSRRequest requestToFetchAllObjectsOfClass:[Post class]];
request.queryParameters = @{@"q":@"search"};
//GET to /posts?q=search
id response = [request sendSynchronous:&e];
Tasks
Properties
-
config
Config used to make the request.
property -
route
The path to be appended to config’s rootURL (the base URL of your app).
property -
httpMethod
The HTTP verb with which to make the request.
property -
queryParameters
The query parameters with which to make the request.
property -
additionalHTTPHeaders
A dictionary of additional HTTP headers to send with the request.
property -
body
Request body.
property
Creating an NSRRequest by HTTP method
-
+ GET
Creates and returns an NSRRequest object initialized with
httpMethod
set to@"GET"
. -
+ DELETE
Creates and returns an NSRRequest object initialized with
httpMethod
set to@"DELETE"
. -
+ POST
Creates and returns an NSRRequest object initialized with
httpMethod
set to@"POST"
. -
+ PUT
Creates and returns an NSRRequest object initialized with
httpMethod
set to@"PUT"
. -
+ PATCH
Creates and returns an NSRRequest object initialized with
httpMethod
set to@"PATCH"
.
Creating an NSRRequest by function
-
+ requestToFetchObjectWithID:ofClass:
Creates and returns an NSRRequest object set to fetch an object with a specified ID.
-
+ requestToFetchAllObjectsOfClass:
Creates and returns an NSRRequest object set to fetch all objects of a given class.
-
+ requestToFetchAllObjectsOfClass:viaObject:
Creates and returns an NSRRequest object set to fetch all objects of a given class given a parent object.
-
+ requestToCreateObject:
Creates and returns an NSRRequest object set to remotely create a given object.
-
+ requestToFetchObject:
Creates and returns an NSRRequest object set to fetch a given object’s remote correspondance.
-
+ requestToDestroyObject:
Creates and returns an NSRRequest object set to remotely destroy a given object.
-
+ requestToUpdateObject:
Creates and returns an NSRRequest object set to remotely update a given object.
-
+ requestToReplaceObject:
Creates and returns an NSRRequest object set to remotely “put” (replace) a given object.
Routing a request
-
– routeTo:
Routes the request to the given string.
-
– routeToClass:
Routes the request to a given class’s controller.
-
– routeToObject:
Routes the request to a given object.
-
– routeToClass:withCustomMethod:
Routes the request to a given class’s controller.
-
– routeToObject:withCustomMethod:
Routes the request to a given object.
Setting the request body
-
– setBodyToObject:
Sets the body to a given object.
Sending the request
-
– sendSynchronous:
Sends the request synchronously.
-
– sendAsynchronous:
Sends the request asynchronously.
Properties
additionalHTTPHeaders
A dictionary of additional HTTP headers to send with the request.
@property (nonatomic, strong) NSDictionary *additionalHTTPHeaders
Declared In
NSRRequest.h
body
Request body.
@property (nonatomic, strong) id body
Discussion
Must be a JSON-parsable object (NSArray, NSDictionary, NSString) or will throw an exception.
Declared In
NSRRequest.h
config
Config used to make the request.
@property (nonatomic, strong) NSRConfig *config
Discussion
This property is automatically set when a routeTo
method is invoked (will set it to the config of the class/instance).
Will use NSRConfig’s defaultConfig
by default (ie, is not routed to an object or class).
When this request is made, any additionalHTTPHeaders defined for this config are added to the request header (along with this instance’s own additionalHTTPHeaders).
Declared In
NSRRequest.h
httpMethod
The HTTP verb with which to make the request.
@property (nonatomic, readonly) NSString *httpMethod
Declared In
NSRRequest.h
queryParameters
The query parameters with which to make the request.
@property (nonatomic, strong) NSDictionary *queryParameters
Discussion
NSRRequest *request = [[NSRRequest GET] routeTo:@"something"];
request.queryParameters = @{@"q":@"search"};
//GET to /something?q=search
id response = [request sendSynchronous:&e];
Warning: Doesn’t escape anything! Make sure your params are RFC 1808 compliant.
Declared In
NSRRequest.h
route
The path to be appended to config’s rootURL (the base URL of your app).
@property (nonatomic, readonly) NSString *route
Discussion
You cannot set this variable directly - use the routeTo
methods below.
Declared In
NSRRequest.h
Class Methods
DELETE
Creates and returns an NSRRequest object initialized with httpMethod
set to @"DELETE"
.
+ (NSRRequest *)DELETE
Return Value
An NSRRequest object initialized with httpMethod
set to @"DELETE"
.
Declared In
NSRRequest.h
GET
Creates and returns an NSRRequest object initialized with httpMethod
set to @"GET"
.
+ (NSRRequest *)GET
Return Value
An NSRRequest object initialized with httpMethod
set to @"GET"
.
Declared In
NSRRequest.h
PATCH
Creates and returns an NSRRequest object initialized with httpMethod
set to @"PATCH"
.
+ (NSRRequest *)PATCH
Return Value
An NSRRequest object initialized with httpMethod
set to @"PATCH"
.
Declared In
NSRRequest.h
POST
Creates and returns an NSRRequest object initialized with httpMethod
set to @"POST"
.
+ (NSRRequest *)POST
Return Value
An NSRRequest object initialized with httpMethod
set to @"POST"
.
Declared In
NSRRequest.h
PUT
Creates and returns an NSRRequest object initialized with httpMethod
set to @"PUT"
.
+ (NSRRequest *)PUT
Return Value
An NSRRequest object initialized with httpMethod
set to @"PUT"
.
Declared In
NSRRequest.h
requestToCreateObject:
Creates and returns an NSRRequest object set to remotely create a given object.
+ (NSRRequest *)requestToCreateObject:(NSRRemoteObject *)obj
Parameters
- obj
Object you wish to create.
Return Value
An NSRRequest object set to remotely create a given object.
Discussion
POST
request routed to the given object (ID is ignored).
POST /posts
Or, if NSRRemoteObject’s objectUsedToPrefixRequest:
is overriden and returns a non-nil object,
POST /users/3/posts
Declared In
NSRRequest.h
requestToDestroyObject:
Creates and returns an NSRRequest object set to remotely destroy a given object.
+ (NSRRequest *)requestToDestroyObject:(NSRRemoteObject *)obj
Parameters
- obj
Object you wish to fetch.
Return Value
An NSRRequest object set to destroy a given object’s remote correspondance.
Discussion
DELETE
request routed to the given object.
DELETE /posts/1
Or, if NSRRemoteObject’s objectUsedToPrefixRequest:
is overriden and returns a non-nil object,
DELETE /users/3/posts/1
Declared In
NSRRequest.h
requestToFetchAllObjectsOfClass:
Creates and returns an NSRRequest object set to fetch all objects of a given class.
+ (NSRRequest *)requestToFetchAllObjectsOfClass:(Class)class
Parameters
- class
Class of the object you wish to fetch. Must be an NSRRemoteObject subclass.
Return Value
An NSRRequest object set to fetch all objects of a given class.
Discussion
GET
request routed to the given class.
GET /posts
Declared In
NSRRequest.h
requestToFetchAllObjectsOfClass:viaObject:
Creates and returns an NSRRequest object set to fetch all objects of a given class given a parent object.
+ (NSRRequest *)requestToFetchAllObjectsOfClass:(Class)class viaObject:(NSRRemoteObject *)obj
Parameters
- class
Class of the object you wish to fetch. Must be an NSRRemoteObject subclass.
- obj
Parent object used to prefix the route.
Return Value
An NSRRequest object set to fetch all objects of a given class given a parent object.
Discussion
GET
request routed to the given parent object, the custom method being the class’s index page.
GET /users/3/posts
Declared In
NSRRequest.h
requestToFetchObject:
Creates and returns an NSRRequest object set to fetch a given object’s remote correspondance.
+ (NSRRequest *)requestToFetchObject:(NSRRemoteObject *)obj
Parameters
- obj
Object you wish to fetch.
Return Value
An NSRRequest object set to fetch a given object’s remote correspondance.
Discussion
GET
request routed to the given object.
GET /posts/1
Or, if NSRRemoteObject’s objectUsedToPrefixRequest:
is overriden and returns a non-nil object,
GET /users/3/posts/1
Declared In
NSRRequest.h
requestToFetchObjectWithID:ofClass:
Creates and returns an NSRRequest object set to fetch an object with a specified ID.
+ (NSRRequest *)requestToFetchObjectWithID:(NSNumber *)remoteID ofClass:(Class)class
Parameters
- remoteID
Remote ID of the object you wish to fetch. Will raise an exception if this is
nil
.
- class
Class of the object you wish to fetch. Must be an NSRRemoteObject subclass.
Return Value
An NSRRequest object set to fetch an object with a specified ID.
Discussion
GET
request routed to the given class, the custom method being the remoteID.
GET /posts/1
Declared In
NSRRequest.h
requestToReplaceObject:
Creates and returns an NSRRequest object set to remotely “put” (replace) a given object.
+ (NSRRequest *)requestToReplaceObject:(NSRRemoteObject *)obj
Parameters
- obj
Object you wish to update.
Return Value
An NSRRequest object set to remotely “put” (replace) a given object.
Discussion
PUT
request routed to the given object.
PUT /posts/1
Or, if NSRRemoteObject’s objectUsedToPrefixRequest:
is overriden and returns a non-nil object,
PUT /users/3/posts/1
Declared In
NSRRequest.h
requestToUpdateObject:
Creates and returns an NSRRequest object set to remotely update a given object.
+ (NSRRequest *)requestToUpdateObject:(NSRRemoteObject *)obj
Parameters
- obj
Object you wish to update.
Return Value
An NSRRequest object set to remotely update a given object.
Discussion
Request routed to the given object. HTTP method depends on config’s updateMethod
.
(PUT, PATCH, etc) /posts/1
Or, if NSRRemoteObject’s objectUsedToPrefixRequest:
is overriden and returns a non-nil object,
(PUT, PATCH, etc) /users/3/posts/1
Declared In
NSRRequest.h
Instance Methods
routeTo:
Routes the request to the given string.
- (NSRRequest *)routeTo:(NSString *)route
Parameters
- route
The route.
Declared In
NSRRequest.h
routeToClass:
Routes the request to a given class’s controller.
- (NSRRequest *)routeToClass:(Class)class
Parameters
- class
Class to which to route. Must be an NSRRemoteObject subclass.
Declared In
NSRRequest.h
routeToClass:withCustomMethod:
Routes the request to a given class’s controller.
- (NSRRequest *)routeToClass:(Class)class withCustomMethod:(NSString *)customMethod
Parameters
- class
Class to route to. Must be an NSRRemoteObject subclass.
- customMethod
Custom method to be appended to the class’s controller RESTfully (can be
nil
.)
Declared In
NSRRequest.h
routeToObject:
Routes the request to a given object.
- (NSRRequest *)routeToObject:(NSRRemoteObject *)object
Parameters
- object
Object to route to. If this object’s
remoteID
isnil
, will only route to its class.
Declared In
NSRRequest.h
routeToObject:withCustomMethod:
Routes the request to a given object.
- (NSRRequest *)routeToObject:(NSRRemoteObject *)object withCustomMethod:(NSString *)customMethod
Parameters
- object
Object to route to. If this object’s
remoteID
isnil
, will only route to its class.
- customMethod
Custom method to be appended RESTfully (can be
nil
.)
Return Value
The request itself. Done to allow concise constructions (see above).
Declared In
NSRRequest.h
sendAsynchronous:
Sends the request asynchronously.
- (void)sendAsynchronous:(NSRHTTPCompletionBlock)completionBlock
Parameters
- completionBlock
Block to be executed when the request is complete.
Discussion
Handles Rails errors, as well as basic connection errors.
Declared In
NSRRequest.h
sendSynchronous:
Sends the request synchronously.
- (id)sendSynchronous:(NSError **)error
Parameters
- error
Out parameter used if an error occurs while processing the request. May be
NULL
, but please check for errors.
Return Value
JSON response object.
Discussion
Handles Rails errors, as well as basic connection errors.
Declared In
NSRRequest.h
setBodyToObject:
Sets the body to a given object.
- (void)setBodyToObject:(NSRRemoteObject *)object
Parameters
- object
Object to use as a body to send the request.
Discussion
Will convert the object into a JSON-parsable object (an NSDictionary) by calling NSRRemoteObject’s remoteDictionaryRepresentationWrapped:
on it.
Declared In
NSRRequest.h