JSONAPI.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // JSONAPI.m
  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 "JSONAPI.h"
  15. #pragma mark - helper error model class
  16. @interface JSONAPIRPCErrorModel: JSONModel
  17. @property (assign, nonatomic) int code;
  18. @property (strong, nonatomic) NSString* message;
  19. @property (strong, nonatomic) id<Optional> data;
  20. @end
  21. #pragma mark - static variables
  22. static JSONAPI* sharedInstance = nil;
  23. static long jsonRpcId = 0;
  24. #pragma mark - JSONAPI() private interface
  25. @interface JSONAPI ()
  26. @property (strong, nonatomic) NSString* baseURLString;
  27. @end
  28. #pragma mark - JSONAPI implementation
  29. @implementation JSONAPI
  30. #pragma mark - initialize
  31. +(void)initialize
  32. {
  33. static dispatch_once_t once;
  34. dispatch_once(&once, ^{
  35. sharedInstance = [[JSONAPI alloc] init];
  36. });
  37. }
  38. #pragma mark - api config methods
  39. +(void)setAPIBaseURLWithString:(NSString*)base
  40. {
  41. sharedInstance.baseURLString = base;
  42. }
  43. +(void)setContentType:(NSString*)ctype
  44. {
  45. [JSONHTTPClient setRequestContentType: ctype];
  46. }
  47. #pragma mark - GET methods
  48. +(void)getWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock
  49. {
  50. NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
  51. [JSONHTTPClient getJSONFromURLWithString: fullURL params:params completion:^(NSDictionary *json, JSONModelError *e) {
  52. completeBlock(json, e);
  53. }];
  54. }
  55. #pragma mark - POST methods
  56. +(void)postWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock
  57. {
  58. NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
  59. [JSONHTTPClient postJSONFromURLWithString: fullURL params:params completion:^(NSDictionary *json, JSONModelError *e) {
  60. completeBlock(json, e);
  61. }];
  62. }
  63. #pragma mark - RPC methods
  64. +(void)__rpcRequestWithObject:(id)jsonObject completion:(JSONObjectBlock)completeBlock
  65. {
  66. NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonObject
  67. options:kNilOptions
  68. error:nil];
  69. NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding: NSUTF8StringEncoding];
  70. NSAssert(sharedInstance.baseURLString, @"API base URL not set");
  71. [JSONHTTPClient postJSONFromURLWithString: sharedInstance.baseURLString
  72. bodyString: jsonRequestString
  73. completion:^(NSDictionary *json, JSONModelError* e) {
  74. if (completeBlock) {
  75. //handle the rpc response
  76. NSDictionary* result = json[@"result"];
  77. if (!result) {
  78. JSONAPIRPCErrorModel* error = [[JSONAPIRPCErrorModel alloc] initWithDictionary:json[@"error"] error:nil];
  79. if (error) {
  80. //custom server error
  81. if (!error.message) error.message = @"Generic json rpc error";
  82. e = [JSONModelError errorWithDomain:JSONModelErrorDomain
  83. code:error.code
  84. userInfo: @{ NSLocalizedDescriptionKey : error.message}];
  85. } else {
  86. //generic error
  87. e = [JSONModelError errorBadResponse];
  88. }
  89. }
  90. //invoke the callback
  91. completeBlock(result, e);
  92. }
  93. }];
  94. }
  95. +(void)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args completion:(JSONObjectBlock)completeBlock
  96. {
  97. NSAssert(method, @"No method specified");
  98. if (!args) args = @[];
  99. [self __rpcRequestWithObject:@{
  100. //rpc 1.0
  101. @"id": @(++jsonRpcId),
  102. @"params": args,
  103. @"method": method
  104. } completion:completeBlock];
  105. }
  106. +(void)rpc2WithMethodName:(NSString*)method andParams:(id)params completion:(JSONObjectBlock)completeBlock
  107. {
  108. NSAssert(method, @"No method specified");
  109. if (!params) params = @[];
  110. [self __rpcRequestWithObject:@{
  111. //rpc 2.0
  112. @"jsonrpc": @"2.0",
  113. @"id": @(++jsonRpcId),
  114. @"params": params,
  115. @"method": method
  116. } completion:completeBlock];
  117. }
  118. @end
  119. #pragma mark - helper rpc error model class implementation
  120. @implementation JSONAPIRPCErrorModel
  121. @end