| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // MODynamicBaseUrlAPI.m
- // MiMoLive
- //
- // Created by SuperC on 2023/10/19.
- //
- #import "MODynamicBaseUrlAPI.h"
- #import "MOAPIUrl.h"
- static MODynamicBaseUrlAPI *_sharedClient = nil;
- //默认的请求超时时间
- const static NSTimeInterval kRequestTimeout = 30.0;
- @implementation MODynamicBaseUrlAPI
- + (MODynamicBaseUrlAPI *)clientWithBaseUrl:(NSString *)baseUrl
- {
- if ([baseUrl isEqualToString:@""] ||baseUrl == nil)
- {
- _sharedClient = [[MODynamicBaseUrlAPI alloc] initWithBaseURL:[NSURL URLWithString:kNetPath_Base]];
- }
- else
- {
- _sharedClient = [[MODynamicBaseUrlAPI alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];
- }
- return _sharedClient;
- }
- - (id)initWithBaseURL:(NSURL *)url
- {
- self = [super initWithBaseURL:url];
- if (!self)
- {
- return nil;
- }
-
- self.responseSerializer = [AFJSONResponseSerializer serializer];
- self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html",nil];
-
- self.requestSerializer = [AFJSONRequestSerializer serializer];
- self.requestSerializer.timeoutInterval = kRequestTimeout;
-
- //证书校验模式为None
- AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
- securityPolicy.allowInvalidCertificates = YES;
- securityPolicy.validatesDomainName = NO;
- self.securityPolicy = securityPolicy;
- return self;
- }
- - (NSURLSessionDataTask *)requestJsonDataWithPath:(NSString *)aPath withData:(NSData *)data withType:(NSString *)type withParams:(NSDictionary *)params withMethodType:(int)networkMethodInt withProgressHandler:(void (^)(NSProgress *))progressHandler andBlock:(void (^)(id, NSError *))block
- {
- self.requestSerializer.timeoutInterval = 600;
-
- NSURLSessionDataTask *task = nil;
- switch (networkMethodInt)
- {
- case Post:
- {
- //DDLogDebug(@"-----> request chatserver[post] %@\n params:%@\n headers:%@",aPath,params,self.requestSerializer.HTTPRequestHeaders);
- task = [self POST:aPath parameters:params headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
- {
- if (data)
- {
- if ([type hasPrefix:@"image"] || [type hasPrefix:@"f_image"])
- {
- NSString *hashString = [NSString stringWithFormat:@"%lu",(unsigned long)[data hash]];
- NSString *temp = [type stringByReplacingOccurrencesOfString:@"image/" withString:@""];
- [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%@",hashString] mimeType:type];
- }
- }
- }
- progress:^(NSProgress * _Nonnull uploadProgress)
- {
- progressHandler?progressHandler(uploadProgress):nil;
- }
- success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
- {
- id error = [self handleResponse:responseObject];
- if (error)
- {
-
- block(nil, error);
- }
- else
- {
- block(responseObject, nil);
- }
- }
- failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
- {
- block(nil, error);
- }];
- break;
- }
- case Put:
- {
- NSString *hashString = [NSString stringWithFormat:@"%lu",(unsigned long)[data hash]];
- NSString *temp = [type stringByReplacingOccurrencesOfString:@"image/" withString:@""];
-
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:aPath parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
-
- } error:nil];
-
- NSDictionary *headers = @{
- @"Content-Type": type
- };
- [request setAllHTTPHeaderFields:headers];
- task = [manager uploadTaskWithRequest:request fromData:data progress:^(NSProgress * _Nonnull uploadProgress) {
- progressHandler?progressHandler(uploadProgress):nil;
- } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
- if (!error) {
- block(responseObject, nil);
- } else {
- block(nil, error);
- }
- }];
- [task resume];
- break;
- }
- }
- return task;
- }
- #pragma mark -- 错误码处理 --
- - (id)handleResponse:(id)responseJSON
- {
- NSError *error = nil;
-
- if ([responseJSON isKindOfClass:[NSArray class]])
- {
- return error;
- }
-
- //code为非0值时,表示有错
- NSNumber *resultCode = [responseJSON valueForKeyPath:@"ErrorCode"];
- if (resultCode.intValue!=0)
- {
- error = [NSError errorWithDomain:kNetPath_Base code:resultCode.intValue userInfo:responseJSON];
- return error;
- }
- return error;
- }
- @end
|