Hibit_iOS/HiBit/Project/Services/HBNetWorkingUtility.m
2024-06-07 13:38:31 +08:00

254 lines
10 KiB
Objective-C

//
// HBNetWorkingUtility.m
// QinJiuTV
//
// Created by 秦九 on 2024/4/29.
//
#import "HBNetWorkingUtility.h"
#import "NSString+Extension.h"
@implementation HBNetWorkingUtility
/// POST 请求
/// - Parameters:
/// - api: 这里不能填完整url 只需要填 接口api
/// - parameters:参数
/// - responseBlock: 请求回调
+ (void)postRequestWithApi:(nonnull NSString *)api
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock {
[self requestWithHTTPMethod:HTTPMethodPost
api:api
authorization:nil
parameters:parameters
response:responseBlock];
}
+ (void)postRequestWithApi:(nonnull NSString *)api
headers:(nullable NSDictionary *)headers
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock {
[self requestWithHTTPMethod:HTTPMethodPost
api:api
headers:headers
parameters:parameters
response:responseBlock];
}
/// 数据请求
/// - Parameters:
/// - method: 请求方式
/// - api: 这里不能填完整url 只需要填 接口api
/// - authorization: authorization
/// - parameters: 参数
/// - responseBlock: 请求回调
+ (void)tokenRequestWithHTTPMethod:(HTTPMethod)method
api:(nonnull NSString *)api
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
NSString * url = [NSString stringWithFormat:@"%@/%@",HBBaseUrlString,api];
NSString * token = [NSUserDefaults stringForKey:HBNetWorkingTokenKey];
BOOL flag = ![NSString empty:token];
[self requestWithHTTPMethod:method
urlString:url
authorization:flag ? token : @""
parameters:parameters
response:responseBlock];
}
/// 数据请求
/// - Parameters:
/// - method: 请求方式
/// - api: 这里不能填完整url 只需要填 接口api
/// - authorization: authorization
/// - parameters: 参数
/// - responseBlock: 请求回调
+ (void)requestWithHTTPMethod:(HTTPMethod)method
api:(nonnull NSString *)api
authorization:(nullable NSString *)authorization
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
NSString * url = [NSString stringWithFormat:@"%@/%@",HBBaseUrlString,api];
[self requestWithHTTPMethod:method
urlString:url
authorization:authorization
parameters:parameters
response:responseBlock];
}
/// 数据请求
/// - Parameters:
/// - method: 请求方式
/// - api: 这里不能填完整url 只需要填 接口api
/// - authorization: authorization
/// - parameters: 参数
/// - responseBlock: 请求回调
+ (void)requestWithHTTPMethod:(HTTPMethod)method
api:(nonnull NSString *)api
headers:(nullable NSDictionary *)headers
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock {
NSString * url = [NSString stringWithFormat:@"%@/%@",HBBaseUrlString,api];
[self requestWithHTTPMethod:method
urlString:url
headers:headers
parameters:parameters
response:responseBlock];
}
/// 数据请求
/// - Parameters:
/// - method: 请求方式
/// - urlString: 这里需要填写完整的url
/// - authorization: authorization
/// - parameters: 参数
/// - responseBlock: 请求回调
+ (void)requestWithHTTPMethod:(HTTPMethod)method
urlString:(nonnull NSString *) urlString
authorization:(nullable NSString *)authorization
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
[self requestWithHTTPMethod:method
urlString:urlString
headers:@{@"Authorization":authorization}
parameters:parameters
response:responseBlock];
}
/// 数据请求
/// - Parameters:
/// - method: 请求方式
/// - urlString: 这里需要填写完整的url
/// - headers: headers
/// - parameters: 参数
/// - responseBlock: 请求回调
+ (void)requestWithHTTPMethod:(HTTPMethod)method
urlString:(nonnull NSString *)urlString
headers:(nullable NSDictionary *)headers
parameters:(nullable NSDictionary *)parameters
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock {
[HtyoNetWorking requestWithHTTPMethod:method
URLString:urlString
headers:headers
parameters:parameters
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseBlock) {
HBNetWorkingUtilityResult * resut = [HBNetWorkingUtilityResult successResult:task response:responseObject];
if (resut.code == 403) {
NSLog(@"newworking toke 失效!");
}
responseBlock(resut);
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (responseBlock) {
responseBlock([HBNetWorkingUtilityResult failureResult:task error:error]);
}
}];
}
/// POST 上传文件请求
/// - Parameters:
/// - api: 这里不能填完整url 只需要填 接口api
/// - parameters: 参数
/// - block: 需要上传的文件 在block里 配置
/// - uploadProgress: 上传进度
/// - responseBlock: 请求回调
+ (void)postRequestWithApi:(nonnull NSString *)api
parameters:(nullable NSDictionary *)parameters
constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
[self postRequestWithApi:api
authorization:nil
parameters:parameters
constructingBodyWithBlock:block
progress:uploadProgress
response:responseBlock];
}
/// POST 上传文件请求
/// - Parameters:
/// - api: 这里不能填完整url 只需要填 接口api
/// - authorization: authorization
/// - parameters: 参数
/// - block: 需要上传的文件 在block里 配置
/// - uploadProgress: 上传进度
/// - responseBlock: 请求回调
+ (void)postRequestWithApi:(nonnull NSString *)api
authorization:(nullable NSString *)authorization
parameters:(nullable NSDictionary *)parameters
constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
NSString * url = [NSString stringWithFormat:@"%@/%@",HBBaseUrlString,api];
[self postRequestWitUrlString:url
authorization:authorization
parameters:parameters
constructingBodyWithBlock:block
progress:uploadProgress
response:responseBlock];
}
/// POST 上传文件请求
/// - Parameters:
/// - urlString: 这里需要完整的url
/// - authorization: authorization
/// - parameters: 参数
/// - block: 需要上传的文件 在block里 配置
/// - uploadProgress: 上传进度
/// - responseBlock: 请求回调
+ (void)postRequestWitUrlString:(nonnull NSString *)urlString
authorization:(nullable NSString *)authorization
parameters:(nullable NSDictionary *)parameters
constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
response:(nullable void (^)(HBNetWorkingUtilityResult * _Nullable result))responseBlock{
[HtyoNetWorking postRequestWithURLString:urlString
authorization:authorization
parameters:parameters
constructingBodyWithBlock:block
progress:uploadProgress
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseBlock) {
responseBlock([HBNetWorkingUtilityResult successResult:task response:responseObject]);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (responseBlock) {
responseBlock([HBNetWorkingUtilityResult failureResult:task error:error]);
}
}];
}
@end
/*
+ (void)saveCookieIfHave:(NSURLSessionDataTask *)task {
NSString *cookieString = [[(NSHTTPURLResponse *)task.response allHeaderFields] valueForKey:@"Set-Cookie"];
NSMutableString *finalCookie = [NSMutableString new];
NSArray *cookieStrings = [cookieString componentsSeparatedByString:@","];
for (NSString *temp in cookieStrings) {
NSArray *tempArr = [temp componentsSeparatedByString:@";"];
[finalCookie appendString:[NSString stringWithFormat:@"%@;", tempArr[0]]];
}
if (finalCookie.length > 0) {
// [DEFAULTS setObject:finalCookie forKey:RCDUserCookiesKey];
// [DEFAULTS synchronize];
[[HtyoNetWorking sharedManager].requestSerializer setValue:finalCookie forHTTPHeaderField:@"Cookie"];
}
}
*/