JSONHTTPClient.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // JSONModelHTTPClient.h
  3. //
  4. // @version 1.0.0
  5. // @author Marin Todorov, http://www.touch-code-magazine.com
  6. //
  7. // Copyright (c) 2012-2014 Marin Todorov, Underplot ltd.
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. //
  14. // The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
  15. #import "JSONModel.h"
  16. #import <UIKit/UIKit.h>
  17. #pragma mark - definitions
  18. /**
  19. * HTTP Request methods
  20. */
  21. extern NSString* const kHTTPMethodGET;
  22. extern NSString* const kHTTPMethodPOST;
  23. /**
  24. * Content-type strings
  25. */
  26. extern NSString* const kContentTypeAutomatic;
  27. extern NSString* const kContentTypeJSON;
  28. extern NSString* const kContentTypeWWWEncoded;
  29. /**
  30. * A block type to handle incoming JSON object and an error.
  31. * You pass it to methods which fetch JSON asynchroniously. When the operation is finished
  32. * you receive back the fetched JSON (or nil) and an error (or nil)
  33. *
  34. * @param json object derived from a JSON string
  35. * @param err JSONModelError or nil
  36. */
  37. typedef void (^JSONObjectBlock)(id json, JSONModelError* err);
  38. /////////////////////////////////////////////////////////////////////////////////////////////
  39. #pragma mark - configuration methods
  40. /**
  41. * @discussion A very thin HTTP client that can do GET and POST HTTP requests.
  42. * It fetches only JSON data and also deserializes it using NSJSONSerialization.
  43. */
  44. @interface JSONHTTPClient : NSObject
  45. /////////////////////////////////////////////////////////////////////////////////////////////
  46. /** @name HTTP Client configuration */
  47. /**
  48. * Returns a modifyable dictionary of the client's default HTTP headers.
  49. * @result A mutable dictionary of pairs - HTTP header names and values.
  50. * @discussion You can use the result to modify the http client headers like so:
  51. * <pre>
  52. * NSMutableDictionary* headers = [JSONHTTPClient requestHeaders];
  53. * headers[@"APIToken"] = @"MySecretTokenValue";
  54. * </pre>
  55. */
  56. +(NSMutableDictionary*)requestHeaders;
  57. /**
  58. * Sets the default encoding of the request body.
  59. * See NSStringEncoding for a list of supported encodings
  60. * @param encoding text encoding constant
  61. */
  62. +(void)setDefaultTextEncoding:(NSStringEncoding)encoding;
  63. /**
  64. * Sets the policies for caching HTTP data
  65. * See NSURLRequestCachePolicy for a list of the pre-defined policies
  66. * @param policy the caching policy
  67. */
  68. +(void)setCachingPolicy:(NSURLRequestCachePolicy)policy;
  69. /**
  70. * Sets the timeout for network calls
  71. * @param seconds the amount of seconds to wait before considering the call failed
  72. */
  73. +(void)setTimeoutInSeconds:(int)seconds;
  74. /**
  75. * A method to enable/disable automatic network indicator showing.
  76. * Set to YES by default.
  77. * @param doesControlIndicator if YES, the library shows and hides the
  78. * system network indicator automatically on begin and end of
  79. * network operations
  80. */
  81. +(void)setControlsNetworkIndicator:(BOOL)doesControlIndicator;
  82. /**
  83. * A method to set the default conent type of the request body
  84. * By default the content type is set to kContentTypeAutomatic
  85. * which checks the body request and decides between "application/json"
  86. * and "application/x-www-form-urlencoded"
  87. */
  88. +(void)setRequestContentType:(NSString*)contentTypeString;
  89. /////////////////////////////////////////////////////////////////////////////////////////////
  90. #pragma mark - GET asynchronious JSON calls
  91. /** @name Making asynchronious HTTP requests */
  92. /**
  93. * Makes GET request to the given URL address and fetches a JSON response.
  94. * @param urlString the URL as a string
  95. * @param completeBlock JSONObjectBlock to execute upon completion
  96. */
  97. +(void)getJSONFromURLWithString:(NSString*)urlString completion:(JSONObjectBlock)completeBlock;
  98. /**
  99. * Makes GET request to the given URL address and fetches a JSON response. Sends the params as a query string variables.
  100. * @param urlString the URL as a string
  101. * @param params a dictionary of key / value pairs to be send as variables to the request
  102. * @param completeBlock JSONObjectBlock to execute upon completion
  103. */
  104. +(void)getJSONFromURLWithString:(NSString*)urlString params:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock;
  105. /**
  106. * Makes a request to the given URL address and fetches a JSON response.
  107. * @param urlString the URL as a string
  108. * @param method the method of the request as a string
  109. * @param params a dictionary of key / value pairs to be send as variables to the request
  110. * @param bodyString the body of the POST request as a string
  111. * @param completeBlock JSONObjectBlock to execute upon completion
  112. */
  113. +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method params:(NSDictionary*)params orBodyString:(NSString*)bodyString completion:(JSONObjectBlock)completeBlock;
  114. /**
  115. * Makes a request to the given URL address and fetches a JSON response.
  116. * @param urlString the URL as a string
  117. * @param method the method of the request as a string
  118. * @param params a dictionary of key / value pairs to be send as variables to the request
  119. * @param bodyString the body of the POST request as a string
  120. * @param headers the headers to set on the request - overrides those in +requestHeaders
  121. * @param completeBlock JSONObjectBlock to execute upon completion
  122. */
  123. +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method params:(NSDictionary*)params orBodyString:(NSString*)bodyString headers:(NSDictionary*)headers completion:(JSONObjectBlock)completeBlock;
  124. /**
  125. * Makes a request to the given URL address and fetches a JSON response.
  126. * @param urlString the URL as a string
  127. * @param method the method of the request as a string
  128. * @param params a dictionary of key / value pairs to be send as variables to the request
  129. * @param bodyData the body of the POST request as raw binary data
  130. * @param headers the headers to set on the request - overrides those in +requestHeaders
  131. * @param completeBlock JSONObjectBlock to execute upon completion
  132. */
  133. +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method params:(NSDictionary *)params orBodyData:(NSData*)bodyData headers:(NSDictionary*)headers completion:(JSONObjectBlock)completeBlock;
  134. /////////////////////////////////////////////////////////////////////////////////////////////
  135. #pragma mark - POST synchronious JSON calls
  136. /**
  137. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  138. * @param urlString the URL as a string
  139. * @param params a dictionary of key / value pairs to be send as variables to the request
  140. * @param completeBlock JSONObjectBlock to execute upon completion
  141. */
  142. +(void)postJSONFromURLWithString:(NSString*)urlString params:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock;
  143. /**
  144. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  145. * @param urlString the URL as a string
  146. * @param bodyString the body of the POST request as a string
  147. * @param completeBlock JSONObjectBlock to execute upon completion
  148. */
  149. +(void)postJSONFromURLWithString:(NSString*)urlString bodyString:(NSString*)bodyString completion:(JSONObjectBlock)completeBlock;
  150. /**
  151. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  152. * @param urlString the URL as a string
  153. * @param bodyData the body of the POST request as an NSData object
  154. * @param completeBlock JSONObjectBlock to execute upon completion
  155. */
  156. +(void)postJSONFromURLWithString:(NSString*)urlString bodyData:(NSData*)bodyData completion:(JSONObjectBlock)completeBlock;
  157. @end