JSONHTTPClient.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // JSONModelHTTPClient.h
  3. //
  4. // @version 1.2
  5. // @author Marin Todorov (http://www.underplot.com) and contributors
  6. //
  7. // Copyright (c) 2012-2015 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. #import "JSONModel.h"
  15. #pragma mark - definitions
  16. /**
  17. * HTTP Request methods
  18. */
  19. extern NSString* const kHTTPMethodGET;
  20. extern NSString* const kHTTPMethodPOST;
  21. /**
  22. * Content-type strings
  23. */
  24. extern NSString* const kContentTypeAutomatic;
  25. extern NSString* const kContentTypeJSON;
  26. extern NSString* const kContentTypeWWWEncoded;
  27. /**
  28. * A block type to handle incoming JSON object and an error.
  29. * You pass it to methods which fetch JSON asynchronously. When the operation is finished
  30. * you receive back the fetched JSON (or nil) and an error (or nil)
  31. *
  32. * @param json object derived from a JSON string
  33. * @param err JSONModelError or nil
  34. */
  35. typedef void (^JSONObjectBlock)(id json, JSONModelError* err);
  36. /////////////////////////////////////////////////////////////////////////////////////////////
  37. #pragma mark - configuration methods
  38. /**
  39. * @discussion A very thin HTTP client that can do GET and POST HTTP requests.
  40. * It fetches only JSON data and also deserializes it using NSJSONSerialization.
  41. */
  42. @interface JSONHTTPClient : NSObject
  43. /////////////////////////////////////////////////////////////////////////////////////////////
  44. /** @name HTTP Client configuration */
  45. /**
  46. * Returns a modifiable dictionary of the client's default HTTP headers.
  47. * @result A mutable dictionary of pairs - HTTP header names and values.
  48. * @discussion You can use the result to modify the http client headers like so:
  49. * <pre>
  50. * NSMutableDictionary* headers = [JSONHTTPClient requestHeaders];
  51. * headers[@"APIToken"] = @"MySecretTokenValue";
  52. * </pre>
  53. */
  54. +(NSMutableDictionary*)requestHeaders;
  55. /**
  56. * Sets the default encoding of the request body.
  57. * See NSStringEncoding for a list of supported encodings
  58. * @param encoding text encoding constant
  59. */
  60. +(void)setDefaultTextEncoding:(NSStringEncoding)encoding;
  61. /**
  62. * Sets the policies for caching HTTP data
  63. * See NSURLRequestCachePolicy for a list of the pre-defined policies
  64. * @param policy the caching policy
  65. */
  66. +(void)setCachingPolicy:(NSURLRequestCachePolicy)policy;
  67. /**
  68. * Sets the timeout for network calls
  69. * @param seconds the amount of seconds to wait before considering the call failed
  70. */
  71. +(void)setTimeoutInSeconds:(int)seconds;
  72. /**
  73. * A method to set the default content type of the request body
  74. * By default the content type is set to kContentTypeAutomatic
  75. * which checks the body request and decides between "application/json"
  76. * and "application/x-www-form-urlencoded"
  77. */
  78. +(void)setRequestContentType:(NSString*)contentTypeString;
  79. /////////////////////////////////////////////////////////////////////////////////////////////
  80. #pragma mark - GET asynchronous JSON calls
  81. /** @name Making asynchronous HTTP requests */
  82. /**
  83. * Makes GET request to the given URL address and fetches a JSON response.
  84. * @param urlString the URL as a string
  85. * @param completeBlock JSONObjectBlock to execute upon completion
  86. */
  87. +(void)getJSONFromURLWithString:(NSString*)urlString completion:(JSONObjectBlock)completeBlock;
  88. /**
  89. * Makes GET request to the given URL address and fetches a JSON response. Sends the params as a query string variables.
  90. * @param urlString the URL as a string
  91. * @param params a dictionary of key / value pairs to be send as variables to the request
  92. * @param completeBlock JSONObjectBlock to execute upon completion
  93. */
  94. +(void)getJSONFromURLWithString:(NSString*)urlString params:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock;
  95. /**
  96. * Makes a request to the given URL address and fetches a JSON response.
  97. * @param urlString the URL as a string
  98. * @param method the method of the request as a string
  99. * @param params a dictionary of key / value pairs to be send as variables to the request
  100. * @param bodyString the body of the POST request as a string
  101. * @param completeBlock JSONObjectBlock to execute upon completion
  102. */
  103. +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method params:(NSDictionary*)params orBodyString:(NSString*)bodyString completion:(JSONObjectBlock)completeBlock;
  104. /**
  105. * Makes a request to the given URL address and fetches a JSON response.
  106. * @param urlString the URL as a string
  107. * @param method the method of the request as a string
  108. * @param params a dictionary of key / value pairs to be send as variables to the request
  109. * @param bodyString the body of the POST request as a string
  110. * @param headers the headers to set on the request - overrides those in +requestHeaders
  111. * @param completeBlock JSONObjectBlock to execute upon completion
  112. */
  113. +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method params:(NSDictionary*)params orBodyString:(NSString*)bodyString headers:(NSDictionary*)headers 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 bodyData the body of the POST request as raw binary data
  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 orBodyData:(NSData*)bodyData headers:(NSDictionary*)headers completion:(JSONObjectBlock)completeBlock;
  124. /////////////////////////////////////////////////////////////////////////////////////////////
  125. #pragma mark - POST asynchronous JSON calls
  126. /**
  127. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  128. * @param urlString the URL as a string
  129. * @param params a dictionary of key / value pairs to be send as variables to the request
  130. * @param completeBlock JSONObjectBlock to execute upon completion
  131. */
  132. +(void)postJSONFromURLWithString:(NSString*)urlString params:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock;
  133. /**
  134. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  135. * @param urlString the URL as a string
  136. * @param bodyString the body of the POST request as a string
  137. * @param completeBlock JSONObjectBlock to execute upon completion
  138. */
  139. +(void)postJSONFromURLWithString:(NSString*)urlString bodyString:(NSString*)bodyString completion:(JSONObjectBlock)completeBlock;
  140. /**
  141. * Makes POST request to the given URL address and fetches a JSON response. Sends the bodyString param as the POST request body.
  142. * @param urlString the URL as a string
  143. * @param bodyData the body of the POST request as an NSData object
  144. * @param completeBlock JSONObjectBlock to execute upon completion
  145. */
  146. +(void)postJSONFromURLWithString:(NSString*)urlString bodyData:(NSData*)bodyData completion:(JSONObjectBlock)completeBlock;
  147. @end