This commit is contained in:
DDIsFriend
2023-08-18 17:28:57 +08:00
commit f0e8a1709d
4282 changed files with 192396 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
//
// DDAF.swift
// DDNetworkingOfAlamofireKit_Private
//
// Created by on 2023/8/2.
//
import Foundation
import Alamofire
import RxSwift
import DDLogKit_Private
public let DDAF = DDAlamofire.default
extension DDAlamofire {
public struct DDParameters : Encodable {
public init() {}
}
// MARK: example
public func get<T:Decodable,P:Encodable>(urlString:String,parameters:P? = DDParameters(),encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
return Single<T?>.create {[weak self] single in
self?.request(urlString: urlString, method: .get,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
self?.logInfo(parameters: parameters, response: response)
single(.success(response.value))
})
return Disposables.create()
}
}
public func post<T:Decodable,P:Encodable>(urlString:String,parameters:P? = DDParameters(),encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
return Single<T?>.create {[weak self] single in
self?.request(urlString: urlString, method: .post,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
self?.logInfo(parameters: parameters, response: response)
single(.success(response.value))
})
return Disposables.create()
}
}
public func get<T:Decodable>(urlString:String,parameters:[String:Any]? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
return Single<T?>.create {[weak self] single in
self?.request(urlString: urlString, method: .get,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
self?.logInfo(parameters: parameters, response: response)
single(.success(response.value))
})
return Disposables.create()
}
}
public func post<T:Decodable>(urlString:String,parameters:[String:Any]? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self) -> Single<T?> {
return Single<T?>.create {[weak self] single in
self?.request(urlString: urlString, method: .post,parameters: parameters,encoding: encoding,headers: headers,responseType: responseType,completionHandler: {[weak self] response in
self?.logInfo(parameters: parameters, response: response)
single(.success(response.value))
})
return Disposables.create()
}
}
private func logInfo<T:Decodable,P:Encodable>(parameters:P?,response:AFDataResponse<T>) {
DDLog(message: (parameters.debugDescription+"\n"+response.debugDescription))
}
private func logInfo<T:Decodable>(parameters:[String:Any]?,response:AFDataResponse<T>) {
DDLog(message: (parameters.debugDescription+"\n"+response.debugDescription))
}
}

View File

@@ -0,0 +1,97 @@
//
// DDAlamofire.swift
// DDNetworkingOfAlamofireKit_Private
//
// Created by on 2023/7/24.
//
import Foundation
import Alamofire
public protocol DDAlamofireDelegate : AnyObject {
func errorCodeHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T>
func networkErrorHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T>
func completionHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T>
}
public extension DDAlamofireDelegate {
func errorCodeHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T> { return response }
func networkErrorHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T> { return response }
func completionHandler<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T> { return response }
}
open class DDAlamofire : NSObject {
// MARK: instance
public static let `default` = DDAlamofire()
public var delegate : DDAlamofireDelegate?
// MARK: configuration
public func request<T: Decodable,P: Encodable>(urlString:String,method:HTTPMethod,parameters:P? = nil,encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) {
let _ = AF.request(urlString: urlString, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: {$0.timeoutInterval = 15}, responseType: responseType) {[weak self] response in
if let response = self?.dealWithResponse(response: response) {
completionHandler(response)
}
}
}
public func request<T: Decodable>(urlString:String,method:HTTPMethod,parameters:[String:Any]? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) {
let _ = AF.request(urlString: urlString, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: {$0.timeoutInterval = 15}, responseType: responseType) {[weak self] response in
if let response = self?.dealWithResponse(response: response) {
completionHandler(response)
}
}
}
public func upload<T: Decodable,P: Encodable>(urlString:String,method:HTTPMethod,parameters:P? = nil,encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,responseType:T.Type = T.self,multipartFormData: @escaping (MultipartFormData) -> Void,uploadProgress: @escaping (Progress) -> Void,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) {
let _ = AF.upload(multipartFormData: { muiltipart in
multipartFormData(muiltipart)
}, urlString: urlString,method: method,headers: headers,requestModifier: {$0.timeoutInterval = 15},uploadProgress: { progress in
uploadProgress(progress)
},responseType: responseType) {[weak self] response in
if let response = self?.dealWithResponse(response: response) {
completionHandler(response)
}
}
}
private func dealWithResponse<T:Decodable>(response:AFDataResponse<T>) -> AFDataResponse<T> {
var returnResponse : AFDataResponse<T>
if let delegate {
if response.response?.statusCode == nil {
returnResponse = delegate.networkErrorHandler(response: response)
}else if response.response?.statusCode != 200 {
returnResponse = delegate.errorCodeHandler(response: response)
}else{
returnResponse = delegate.completionHandler(response: response)
}
}else{
returnResponse = response
}
return returnResponse
}
}
// MARK: AF Extension
extension Session {
// Encodable
public func request<T: Decodable,P: Encodable>(urlString:String,method:HTTPMethod,parameters:P? = nil,encoding:ParameterEncoder = URLEncodedFormParameterEncoder.default,headers:HTTPHeaders? = nil,interceptor:RequestInterceptor? = nil,requestModifier:Session.RequestModifier? = nil,responseType:T.Type = T.self,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) -> DataRequest {
return request(urlString,method: method,parameters: parameters,encoder: encoding,headers: headers,interceptor: interceptor,requestModifier: requestModifier).responseDecodable(of:responseType,queue: .global()) { response in
completionHandler(response)
}
}
//
public func request<T: Decodable>(urlString:String,method:HTTPMethod,parameters:Parameters? = nil,encoding:ParameterEncoding = URLEncoding.default,headers:HTTPHeaders? = nil,interceptor:RequestInterceptor? = nil,requestModifier:Session.RequestModifier? = nil,responseType:T.Type = T.self,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) -> DataRequest {
return request(urlString,method: method,parameters: parameters,encoding: encoding,headers:headers,interceptor:interceptor,requestModifier:requestModifier).responseDecodable(of: responseType,queue: .global()) { response in
completionHandler(response)
}
}
public func upload<T: Decodable>(multipartFormData:@escaping (MultipartFormData) -> Void,urlString:String,usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,method: HTTPMethod = .post,headers: HTTPHeaders? = nil,interceptor: RequestInterceptor? = nil,fileManager: FileManager = .default,requestModifier: RequestModifier? = nil,uploadProgress: @escaping (Progress) -> Void,responseType:T.Type = T.self,completionHandler: @escaping ((AFDataResponse<T>) -> Void)) -> DataRequest {
return upload(multipartFormData: multipartFormData, to: urlString,usingThreshold: encodingMemoryThreshold,method: method,headers: headers,interceptor: interceptor,fileManager: fileManager,requestModifier: requestModifier).uploadProgress(queue: .global(),closure: uploadProgress).responseDecodable(of: responseType,queue: .global()) { response in
completionHandler(response)
}
}
}

View File

@@ -0,0 +1,19 @@
Copyright (c) 2023 DDIsFriend <DDIsFriend@163.com>
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.

View File

@@ -0,0 +1,29 @@
# DDNetworkingOfAlamofireKit_Private
[![CI Status](https://img.shields.io/travis/DDIsFriend/DDNetworkingOfAlamofireKit_Private.svg?style=flat)](https://travis-ci.org/DDIsFriend/DDNetworkingOfAlamofireKit_Private)
[![Version](https://img.shields.io/cocoapods/v/DDNetworkingOfAlamofireKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDNetworkingOfAlamofireKit_Private)
[![License](https://img.shields.io/cocoapods/l/DDNetworkingOfAlamofireKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDNetworkingOfAlamofireKit_Private)
[![Platform](https://img.shields.io/cocoapods/p/DDNetworkingOfAlamofireKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDNetworkingOfAlamofireKit_Private)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
DDNetworkingOfAlamofireKit_Private is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'DDNetworkingOfAlamofireKit_Private'
```
## Author
DDIsFriend, DDIsFriend@163.com
## License
DDNetworkingOfAlamofireKit_Private is available under the MIT license. See the LICENSE file for more info.