123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // JSONAPI.m
- //
- // @version 1.0.0
- // @author Marin Todorov, http://www.touch-code-magazine.com
- //
- // Copyright (c) 2012-2014 Marin Todorov, Underplot ltd.
- // This code is distributed under the terms and conditions of the MIT license.
- //
- // 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:
- // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- // 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.
- //
- // The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
- #import "JSONAPI.h"
- #pragma mark - helper error model class
- @interface JSONAPIRPCErrorModel: JSONModel
- @property (assign, nonatomic) int code;
- @property (strong, nonatomic) NSString* message;
- @property (strong, nonatomic) id<Optional> data;
- @end
- #pragma mark - static variables
- static JSONAPI* sharedInstance = nil;
- static long jsonRpcId = 0;
- #pragma mark - JSONAPI() private interface
- @interface JSONAPI ()
- @property (strong, nonatomic) NSString* baseURLString;
- @end
- #pragma mark - JSONAPI implementation
- @implementation JSONAPI
- #pragma mark - initialize
- +(void)initialize
- {
- static dispatch_once_t once;
- dispatch_once(&once, ^{
- sharedInstance = [[JSONAPI alloc] init];
- });
- }
- #pragma mark - api config methods
- +(void)setAPIBaseURLWithString:(NSString*)base
- {
- sharedInstance.baseURLString = base;
- }
- +(void)setContentType:(NSString*)ctype
- {
- [JSONHTTPClient setRequestContentType: ctype];
- }
- #pragma mark - GET methods
- +(void)getWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock
- {
- NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
-
- [JSONHTTPClient getJSONFromURLWithString: fullURL params:params completion:^(NSDictionary *json, JSONModelError *e) {
- completeBlock(json, e);
- }];
- }
- #pragma mark - POST methods
- +(void)postWithPath:(NSString*)path andParams:(NSDictionary*)params completion:(JSONObjectBlock)completeBlock
- {
- NSString* fullURL = [NSString stringWithFormat:@"%@%@", sharedInstance.baseURLString, path];
-
- [JSONHTTPClient postJSONFromURLWithString: fullURL params:params completion:^(NSDictionary *json, JSONModelError *e) {
- completeBlock(json, e);
- }];
- }
- #pragma mark - RPC methods
- +(void)__rpcRequestWithObject:(id)jsonObject completion:(JSONObjectBlock)completeBlock
- {
-
- NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:jsonObject
- options:kNilOptions
- error:nil];
- NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData encoding: NSUTF8StringEncoding];
- NSAssert(sharedInstance.baseURLString, @"API base URL not set");
- [JSONHTTPClient postJSONFromURLWithString: sharedInstance.baseURLString
- bodyString: jsonRequestString
- completion:^(NSDictionary *json, JSONModelError* e) {
- if (completeBlock) {
- //handle the rpc response
- NSDictionary* result = json[@"result"];
- if (!result) {
- JSONAPIRPCErrorModel* error = [[JSONAPIRPCErrorModel alloc] initWithDictionary:json[@"error"] error:nil];
- if (error) {
- //custom server error
- if (!error.message) error.message = @"Generic json rpc error";
- e = [JSONModelError errorWithDomain:JSONModelErrorDomain
- code:error.code
- userInfo: @{ NSLocalizedDescriptionKey : error.message}];
- } else {
- //generic error
- e = [JSONModelError errorBadResponse];
- }
- }
-
- //invoke the callback
- completeBlock(result, e);
- }
- }];
- }
- +(void)rpcWithMethodName:(NSString*)method andArguments:(NSArray*)args completion:(JSONObjectBlock)completeBlock
- {
- NSAssert(method, @"No method specified");
- if (!args) args = @[];
-
- [self __rpcRequestWithObject:@{
- //rpc 1.0
- @"id": @(++jsonRpcId),
- @"params": args,
- @"method": method
- } completion:completeBlock];
- }
- +(void)rpc2WithMethodName:(NSString*)method andParams:(id)params completion:(JSONObjectBlock)completeBlock
- {
- NSAssert(method, @"No method specified");
- if (!params) params = @[];
-
- [self __rpcRequestWithObject:@{
- //rpc 2.0
- @"jsonrpc": @"2.0",
- @"id": @(++jsonRpcId),
- @"params": params,
- @"method": method
- } completion:completeBlock];
- }
- @end
- #pragma mark - helper rpc error model class implementation
- @implementation JSONAPIRPCErrorModel
- @end
|