ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARConnection.h
Go to the documentation of this file.
1 // ActiveResourceKit ARConnection.h
2 //
3 // Copyright © 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
26 
27 @class ARHTTPResponse;
28 
29 /*!
30  * @brief Defines a lower-level connection-oriented completion handler.
31  * @details The handler accepts a basic URL response, not a HTTP response
32  * necessarily. Though typically, URL responses will be HTTP responses. Apple's
33  * @c NSHTTPURLResponse derives from @c NSURLResponse. The @a data argument
34  * accepts the body data verbatim. When connection completes, the body remains
35  * un-decoded.
36  */
37 typedef void (^ARConnectionCompletionHandler)(ARHTTPResponse *response, NSError *error);
38 
39 /*!
40  * @brief Connects to a site using a format.
41  * @details Connections do not have responsibility for decoding response
42  * bodies. With respect to formatting, connections only carry responsibility for
43  * setting up the correct header fields.
44  */
45 @interface ARConnection : NSObject
46 
47 @property(strong, NS_NONATOMIC_IOSONLY) NSURL *site;
48 
49 @property(strong, NS_NONATOMIC_IOSONLY) id<ARFormat> format;
50 
51 @property(assign, NS_NONATOMIC_IOSONLY) NSTimeInterval timeout;
52 
53 //----------------------------------------------------------------- Initialisers
54 
55 // Parameterised initialisers: the following initialisers just act as shortcuts
56 // for running the default initialiser then sending the site and format using
57 // accessors. Initialising a connection with only a site URL prepares the new
58 // connection for JSON-formatted interactions. This mimics Rails behaviour where
59 // JSON connections initialise by default. You can subsequently change the
60 // format, of course, using -setFormat:newFormat.
61 
62 - (id)initWithSite:(NSURL *)site format:(id<ARFormat>)format;
63 - (id)initWithSite:(NSURL *)site;
64 
65 /*!
66  * @brief Sends a request.
67  * @details Sending a request answers the raw body data. At this level, the
68  * connection only handles the actual sending. It does not attempt to handle the
69  * response. Other object methods interpret the response code and decode the
70  * response body according to the expected format.
71  */
72 - (void)sendRequest:(NSURLRequest *)request completionHandler:(ARConnectionCompletionHandler)completionHandler;
73 
74 /*!
75  * @brief Decides how to handle the given HTTP response based on the response
76  * status code.
77  * @details The Rails implementation of @c handle_response checks the status
78  * code and raises an exception for any status outside the normal range. The
79  * Objective-C implementation does @em not raise exceptions however, by
80  * design. Instead, “handling a response” refers to deriving an error object for
81  * an otherwise-successful response.
82  * @result Answers an @c NSError object if the response indicates a problem; @c
83  * nil otherwise. The error object's error code and localised description
84  * outline the issue. You can also retrieve the HTTP response object itself by
85  * accessing the error's user information dictionary with the @ref
86  * ARConnectionHTTPResponseKey key; this key-value pair captures the given @ref
87  * ARHTTPResponse wrapper which includes the body as well as the original URL
88  * response.
89  */
90 + (NSError *)errorForResponse:(ARHTTPResponse *)response;
91 
92 //------------------------------------------------------------ Building Requests
93 
94 /*!
95  * @brief Builds a mutable HTTP request given a HTTP method, a path and headers.
96  * @details The request site and timeout originates with the message
97  * receiver. You can open either an asynchronous connection using the request,
98  * or send it synchronously and wait for the response. You might even enqueue
99  * the request in a pool of pending requests. This interface method exists and
100  * belongs to the public API in order to provide a flexible approach to handling
101  * connections: asynchronous or synchronous, queued or immediate. The method
102  * simply prepares the request ready to go.
103  * @param HTTPMethod HTTP request method, one of: GET, HEAD, PUT, POST or DELETE.
104  * @param path URL path element to apply to the site's base URL.
105  * @param headers Zero or more header fields. These override all other headers
106  * within the request, including authorisation and formatting headers. Use @c
107  * nil for no headers.
108  */
109 - (NSMutableURLRequest *)requestForHTTPMethod:(NSString *)HTTPMethod path:(NSString *)path headers:(NSDictionary *)headers;
110 
111 @end