ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARService+Private.h
Go to the documentation of this file.
1 // ActiveResourceKit ARService+Private.h
2 //
3 // Copyright © 2011, 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 
27 
28 @class ARResource;
29 @class ARHTTPResponse;
30 
31 /*!
32  * @brief Builds a query string given a dictionary of query options.
33  * @param options Dictionary (a Ruby hash) of query options.
34  * @result The answer is an empty string when you pass @c nil options or the
35  * options indicate an empty dictionary. This function assumes that the given
36  * options are @e query options only; they should not contain prefix options;
37  * otherwise prefix options will appear in the query string. Invoking this
38  * helper function assumes you have already filtered any options by splitting
39  * apart prefix from query options.
40  */
41 NSString *ARQueryStringForOptions(NSDictionary *options);
42 
43 /*!
44  * @brief Defines the request completion handler type.
45  * @details Request completion handlers are C blocks receiving: (1) the HTTP
46  * response, (2) the @a object decoded and (3) the @a error object in case of
47  * error. The response always appears. The object decoded is non-nil if the
48  * response body successfully decodes according to the expected format (JSON or
49  * XML) and error is @c nil. On error, object is @c nil and @a error describes
50  * the error condition. Asynchronous connection methods utilise this completion
51  * handler type. C blocks of this type receive completion results, whether
52  * success or failure. Operation or dispatch queue assignment depends on which
53  * object invokes the block.
54  */
55 typedef void (^ARServiceRequestCompletionHandler)(ARHTTPResponse *response, id object, NSError *error);
56 
58 
59 - (id<ARFormat>)defaultFormat;
60 - (NSString *)defaultElementName;
61 - (NSString *)defaultCollectionName;
62 - (NSString *)defaultPrimaryKey;
63 - (NSString *)defaultPrefixSource;
64 
65 - (void)findEveryWithOptions:(NSDictionary *)options completionHandler:(void (^)(ARHTTPResponse *response, NSArray *resources, NSError *error))completionHandler;
66 
67 /*!
68  * Instantiates a collection of active resources given a collection of
69  * attributes; collection here meaning an array. The collection argument
70  * specifies an array of dictionaries. Each dictionary specifies attributes for
71  * a new active resource. Answers an array of newly instantiated active
72  * resources.
73  */
74 - (NSArray *)instantiateCollection:(NSArray *)collection prefixOptions:(NSDictionary *)prefixOptions;
75 
76 /*!
77  * @brief Instantiates a resource element (a record) using the given attributes
78  * and prefix options.
79  * @details The implementation looks for a class matching the element name. It
80  * converts the element name to class name by camel-casing the name. The answer
81  * becomes an instance of that class if the class exists and derives from
82  * ARResource. Otherwise the answer is a plain instance of ARResource.
83  */
84 - (ARResource *)instantiateRecordWithAttributes:(NSDictionary *)attributes prefixOptions:(NSDictionary *)prefixOptions;
85 
86 /*!
87  * Answers a set of prefix parameters based on the current prefix source. These
88  * constitute the current set of prefix parameters: an array of strings without
89  * the leading colon. Colon immediately followed by a word marks each parameter
90  * in the prefix source.
91  */
92 - (NSSet *)prefixParameters;
93 
94 /*!
95  * Splits an options dictionary into two dictionaries, one containing the prefix
96  * options, the other containing the leftovers, i.e. any query options.
97  */
98 - (void)splitOptions:(NSDictionary *)options prefixOptions:(NSDictionary **)outPrefixOptions queryOptions:(NSDictionary **)outQueryOptions;
99 
100 //---------------------------------------------------------------- HTTP Requests
101 
102 /*!
103  * @brief Sends an asynchronous GET request. Used to find a resource.
104  * @details When the response successfully arrives, the format decodes the
105  * data. If the response body decodes successfully, finally sends the decoded
106  * object (or objects) to your given completion handler. Objects may be hashes
107  * (dictionaries) or arrays, or even primitives.
108  */
109 - (void)get:(NSString *)path completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
110 
111 /*!
112  * @brief Used to delete resources. Sends an asynchronous DELETE request.
113  */
114 - (void)delete:(NSString *)path completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
115 
116 /*!
117  * @brief Sends an asynchronous PUT request.
118  * @details PUT is idempotent, meaning that multiple PUT requests result in an
119  * identical resource state. There should be no side effects. PUT really amounts
120  * to an “upsert” database operation where it updates the resource if it already
121  * exists but alternatively creates the resource if it does not already exist.
122  */
123 - (void)put:(NSString *)path body:(NSData *)data completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
124 
125 /*!
126  * @brief Sends an asynchronous POST request.
127  * @details POST is not idempotent.
128  */
129 - (void)post:(NSString *)path body:(NSData *)data completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
130 
131 /*!
132  * @brief Used to obtain meta-information about resources, whether they exist or
133  * their size. Sends an asynchronous HEAD request.
134  */
135 - (void)head:(NSString *)path completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
136 
137 /*!
138  * @brief Submits an asynchronous request, returning immediately.
139  * @details Constructs a request object and asynchronously transmits it to the
140  * remote RESTful service. The completion handler executes in the resource
141  * service's operation queue, or the current queue (the operation queue running at
142  * the time of the request) if the resource service has no queue.
143  *
144  * The completion handler receives three arguments: the HTTP response, the
145  * decoded object and any error. The decoded object derives from the response
146  * body, decoded according to the service format. Decoding itself can encounter
147  * errors. If successful, the completion handler receives a non-nil object and a
148  * @c nil error. If the response is not an HTTP-based response, the completion
149  * handler receives a @c nil @a response argument and an @ref
150  * ARResponseIsNotHTTPError.
151  */
152 - (void)requestHTTPMethod:(NSString *)HTTPMethod path:(NSString *)path body:(NSData *)data completionHandler:(ARServiceRequestCompletionHandler)completionHandler;
153 
154 /*!
155  * @brief Answers a decoding handler which, in turn, after decoding, invokes
156  * another given completion block.
157  * @details Active Resource Kit methods utilise this common response block for
158  * decoding according to the required format. In fact, the decoding does a
159  * little more than just decode the format. It also handles URL response to HTTP
160  * response type casting. The latter carries more information including header
161  * fields and HTTP status code.
162  */
163 - (ARConnectionCompletionHandler)decodeHandlerWithCompletionHandler:(ARServiceRequestCompletionHandler)completionHandler;
164 
165 @end