PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/MercyMap/Tool/JSONModel/JSONModelNetworking/JSONAPI.m

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