objective c - AFHTTPRequestOperationManager with HTTP and HTTPS base url -



objective c - AFHTTPRequestOperationManager with HTTP and HTTPS base url -

i wondering best alternative (architecture) situation have same api on http , https. there way back upwards http , https requests in same afhttprequestoperationmanager or should have 2 subclasses, 1 http requests , sec https requests?

i sense changing baseurl dynamically not best solution

you can hold 2 afhttpclient* objects. 1 http , other secure https. here illustration based on requester class.

-- requester.h ---

#import "foundation/foundation.h" #import "afnetworking.h" typedef enum { multiparttypeimagejpeg, multiparttypeimagepng, multiparttypevideoquicktime } multiparttype; typedef enum { httpmethodget, httpmethodpost, httpmethodput, httpmethoddelete } httpmethod; typedef void (^requestcallback)(nserror *error, nsinteger statuscode, id json); @interface requester : nsobject /** * singleton */ + (instancetype)sharedinstance; - (void)requesttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params complete:(requestcallback)callback; - (void)requestmultiparttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params filedata:(nsdata *)filedata filename:(nsstring *)filename type:(multiparttype)multyparttype complete:(requestcallback)callback; - (void)securerequesttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params complete:(requestcallback)callback;

-- requester.m ---

#import "requester.h" @interface requester() @property (nonatomic, strong) afhttpclient *httpclient; @property (nonatomic, strong) afhttpclient *httpclientsecure; @end @implementation requester + (instancetype)sharedinstance { static id instance = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ instance = [[self alloc] init]; }); homecoming instance; } + (void)initialize { [super initialize]; nsurl *baseurl = [nsurl urlwithstring:your_base_address_http]; nsurl *securebaseurl = [nsurl urlwithstring:your_base_address_https]; [requester sharedinstance].httpclient = [afhttpclient clientwithbaseurl:baseurl]; [requester sharedinstance].httpclient.parameterencoding = afjsonparameterencoding; [requester sharedinstance].httpclientsecure = [afhttpclient clientwithbaseurl:securebaseurl]; [requester sharedinstance].httpclientsecure.parameterencoding = afjsonparameterencoding; } - (id)init { self = [super init]; if (self) { //... } homecoming self; } #pragma mak - request types - (nsmutableurlrequest*)requestwithpathpost:(nsstring*)path withparams:(nsdictionary*)params { params = [self addrequiredbodyproperties:params]; nsmutableurlrequest *request = [self.httpclient requestwithmethod:@"post" path:path parameters:params]; request.cachepolicy = nsurlrequestreloadignoringcachedata; request = [self addrequiredheaderproperties:request]; homecoming request; } - (nsmutableurlrequest*)requestwithpathget:(nsstring*)path withparams:(nsdictionary*)params { params = [self addrequiredbodyproperties:params]; nsmutableurlrequest *request = [self.httpclient requestwithmethod:@"get" path:path parameters:params]; request.cachepolicy = nsurlrequestreloadignoringcachedata; request = [self addrequiredheaderproperties:request]; homecoming request; } - (nsmutableurlrequest*)requestwithpathput:(nsstring*)path withparams:(nsdictionary*)params { params = [self addrequiredbodyproperties:params]; nsmutableurlrequest *request = [self.httpclient requestwithmethod:@"put" path:path parameters:params]; request.cachepolicy = nsurlrequestreloadignoringcachedata; request = [self addrequiredheaderproperties:request]; homecoming request; } - (nsmutableurlrequest*)requestwithpathdel:(nsstring*)path withparams:(nsdictionary*)params { params = [self addrequiredbodyproperties:params]; nsmutableurlrequest *request = [self.httpclient requestwithmethod:@"delete" path:path parameters:params]; request.cachepolicy = nsurlrequestreloadignoringcachedata; request = [self addrequiredheaderproperties:request]; homecoming request; } - (nsmutableurlrequest *)addrequiredheaderproperties:(nsmutableurlrequest *)request { nsmutabledictionary *dic = [[request allhttpheaderfields] mutablecopy]; // here adding header parameters required on every request nsstring *deviceid = [[[uidevice currentdevice] identifierforvendor] uuidstring]; [dic setobject:deviceid forkey:@"device-id"]; [dic setobject:@"ios" forkey:@"os"]; [request setallhttpheaderfields:dic]; homecoming request; } - (nsmutabledictionary *)addrequiredbodyproperties:(nsdictionary *)params { nsmutabledictionary *result = [params mutablecopy]; if (!result) { result = [nsmutabledictionary new]; } // here adding parameters required on every request if (api_key) { [result setobject:api_key forkey:@"api_key"]; } homecoming result; } - (nsmutableurlrequest*)requestmultipartwithpath:(nsstring*)path method:(nsstring *)method withparams:(nsdictionary*)params filedata:(nsdata *)filedata filename:(nsstring *)filename type:(multiparttype)multyparttype { params = [self addrequiredbodyproperties:params]; nsstring *mimetype = @""; nsstring *name = @"picture"; switch (multyparttype) { case multiparttypeimagejpeg: mimetype = @"image/jpeg"; if (filename) { if ([filename rangeofstring:@".jpg"].location == nsnotfound && [filename rangeofstring:@".jpeg"].location == nsnotfound) { filename = [filename stringbyappendingstring:@".jpg"]; } } break; case multiparttypeimagepng: mimetype = @"image/png"; if (filename) { if ([filename rangeofstring:@".png"].location == nsnotfound) { filename = [filename stringbyappendingstring:@".png"]; } } break; case multiparttypevideoquicktime: mimetype = @"video/quicktime"; name = @"video"; break; } if (!method || [method isequaltostring:@""]) { method = @"post"; } nsmutableurlrequest *request = [self.httpclient multipartformrequestwithmethod:method path:path parameters:params constructingbodywithblock:^(id formdata) { [formdata appendpartwithfiledata:filedata name:name filename:filename mimetype:mimetype]; }]; request = [self addrequiredheaderproperties:request]; homecoming request; } #pragma mark - global requests - (void)jsonrequestoperationwithrequest:(nsmutableurlrequest *)request callback:(requestcallback)callback { [[afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { if (callback) { callback(nil,response.statuscode,o); } } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { nslog(@"%@",error.description); nslog(@"%@",json); if (callback) { callback(error,response.statuscode,json); } }] start]; } #pragma mark - - (void)requesttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params complete:(requestcallback)callback { nsmutableurlrequest *request = nil; switch (method) { case httpmethodget: request = [self requestwithpathget:path withparams:params]; break; case httpmethodput: request = [self requestwithpathput:path withparams:params]; break; case httpmethodpost: request = [self requestwithpathpost:path withparams:params]; break; case httpmethoddelete: request = [self requestwithpathdel:path withparams:params]; break; } [self jsonrequestoperationwithrequest:request callback:callback]; } - (void)requestmultiparttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params filedata:(nsdata *)filedata filename:(nsstring *)filename type:(multiparttype)multyparttype complete:(requestcallback)callback { nsstring *methodstring = @""; switch (method) { case httpmethodget: methodstring = @"get"; break; case httpmethodput: methodstring = @"put"; break; case httpmethodpost: methodstring = @"post"; break; case httpmethoddelete: methodstring = @"delete"; break; } nsmutableurlrequest *request = [self requestmultipartwithpath:path method:methodstring withparams:params filedata:filedata filename:filename type:multyparttype]; [self jsonrequestoperationwithrequest:request callback:callback]; } - (void)securerequesttopath:(nsstring *)path method:(httpmethod)method params:(nsdictionary *)params complete:(requestcallback)callback { nsmutableurlrequest *request = nil; switch (method) { case httpmethodget: request = [self.httpclientsecure requestwithmethod:@"get" path:path parameters:params]; break; case httpmethodput: request = [self.httpclientsecure requestwithmethod:@"put" path:path parameters:params]; break; case httpmethodpost: request = [self.httpclientsecure requestwithmethod:@"post" path:path parameters:params]; break; case httpmethoddelete: request = [self.httpclientsecure requestwithmethod:@"delete" path:path parameters:params]; break; } afjsonrequestoperation *oper = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { if (callback) { callback(nil,response.statuscode,json); } } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { nslog(@"%@",error.description); nslog(@"%@",json); if (callback) { callback(error,response.statuscode,json); } }]; [oper start]; } @end

usage:

for http:

[[requester sharedinstance] requesttopath:@"user/authenticate" method:httpmethodpost params:@{@"username":username,@"password":password} complete:^(nserror *error, nsinteger statuscode, id json) { if (!error) { //do response } }];

for https:

[[requester sharedinstance] securerequesttopath:@"user/authenticate" method:httpmethodpost params:@{@"username":username,@"password":password} complete:^(nserror *error, nsinteger statuscode, id json) { if (!error) { //do response } }];

objective-c cocoa-touch afnetworking afhttprequestoperation

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -