ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARConnection.m
Go to the documentation of this file.
1 // ActiveResourceKit ARConnection.m
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 
25 #import "ARConnection.h"
26 #import "ARConnection+Private.h"
27 
28 #import "ARJSONFormat.h"
29 #import "ARHTTPMethods.h"
30 #import "ARHTTPResponse.h"
31 #import "ARErrors.h"
32 
33 @implementation ARConnection
34 
35 @synthesize site = _site;
36 @synthesize format = _format;
37 @synthesize timeout = _timeout;
38 
39 //------------------------------------------------------------------------------
40 #pragma mark Initialisers
41 //------------------------------------------------------------------------------
42 
43 // designated initialiser
44 - (id)init
45 {
46  self = [super init];
47  if (self)
48  {
49  [self setTimeout:60.0];
50  }
51  return self;
52 }
53 
54 - (id)initWithSite:(NSURL *)site format:(id<ARFormat>)format
55 {
56  self = [self init];
57  if (self)
58  {
59  [self setSite:site];
60  [self setFormat:format];
61  }
62  return self;
63 }
64 
65 - (id)initWithSite:(NSURL *)site
66 {
67  return [self initWithSite:site format:[ARJSONFormat JSONFormat]];
68 }
69 
70 - (void)sendRequest:(NSURLRequest *)request completionHandler:(ARConnectionCompletionHandler)completionHandler
71 {
72  ;
73 }
74 
75 + (NSError *)errorForResponse:(ARHTTPResponse *)response
76 {
77  NSInteger errorCode;
78  switch ([response code])
79  {
80  case 301:
81  case 302:
82  case 303:
83  case 307:
84  // 301: moved permanently (redirect)
85  // 302: found (but redirect)
86  // 303: see other (redirect)
87  // 307: temporarily redirected (redirect)
88  errorCode = ARRedirectionErrorCode;
89  break;
90  case 400:
91  errorCode = ARBadRequestErrorCode;
92  break;
93  case 401:
95  break;
96  case 403:
97  errorCode = ARForbiddenAccessErrorCode;
98  break;
99  case 404:
100  errorCode = ARResourceNotFoundErrorCode;
101  break;
102  case 405:
103  errorCode = ARMethodNotAllowedErrorCode;
104  break;
105  case 409:
106  errorCode = ARResourceConflictErrorCode;
107  break;
108  case 410:
109  errorCode = ARResourceGoneErrorCode;
110  break;
111  case 422:
112  errorCode = ARResourceInvalidErrorCode;
113  break;
114  default:
115  if (200 <= [response code] && [response code] < 400)
116  {
117  errorCode = ' ';
118  }
119  else if (401 <= [response code] && [response code] < 500)
120  {
121  errorCode = ARClientErrorCode;
122  }
123  else if (500 <= [response code] && [response code] < 600)
124  {
125  errorCode = ARServerErrorCode;
126  }
127  else
128  {
129  errorCode = ARConnectionErrorCode;
130  }
131  }
132  NSError *error;
133  if (errorCode != ' ')
134  {
135  NSString *localizedDescription = [NSHTTPURLResponse localizedStringForStatusCode:[response code]];
136  NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:response, ARConnectionHTTPResponseKey, localizedDescription, NSLocalizedDescriptionKey, nil];
137  error = [NSError errorWithDomain:ARConnectionErrorDomain code:errorCode userInfo:userInfo];
138  }
139  else
140  {
141  error = nil;
142  }
143  return error;
144 }
145 
146 //------------------------------------------------------------------------------
147 #pragma mark Building Requests
148 //------------------------------------------------------------------------------
149 
150 - (NSMutableURLRequest *)requestForHTTPMethod:(NSString *)HTTPMethod path:(NSString *)path headers:(NSDictionary *)headers
151 {
152  NSURL *URL = [NSURL URLWithString:path relativeToURL:[self site]];
153  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:[self timeout]];
154  NSMutableDictionary *headerFields = [NSMutableDictionary dictionaryWithDictionary:[request allHTTPHeaderFields]];
155  [headerFields addEntriesFromDictionary:[self buildRequestHeaderFieldsUsingHeaders:headers forHTTPMethod:HTTPMethod]];
156  [request setAllHTTPHeaderFields:headerFields];
157  [request setHTTPMethod:HTTPMethod];
158  return request;
159 }
160 
161 @end