如何使用 get-API(具有以下请求结构)与 Moya 一起使用?
http://some.environment.com/some/api/some/contacts/81193?types=["details", "permissions"]
这是我尝试过的。
enum MyApiTarget: TargetType {
case getInfo(contactID: Int, types: [String])
public var baseURL: URL {
switch self {
case .getInfo:
return URL(string: "http://some.environment.com/some/api")!
}
}
public var path: String {
switch self {
case .getInfo(contactID: let contactId, types: _):
return "/some/contacts/(contactId)"
}
}
public var method: Moya.Method {
switch self {
case .getInfo:
return .get
}
}
public var sampleData: Data {
return Data()
}
public var task: Task {
switch self {
case .getInfo(contactID: _, types: let types):
return .requestParameters(
parameters: ["types" : types],
encoding: URLEncoding.queryString
)
}
}
public var headers: [String: String]? {
return nil
}
}
上面的代码生成以下 URL。
http://some.environment.com/some/api/some/contacts/81193?types%5B%5D=details&types%5B%5D=permissions
我尝试了以下编码
URLEncoding.queryString
URLEncoding.default
URLEncoding.httpBody
JSONEncoding.default
JSONEncoding.prettyPrinted
没有一个编码帮助我产生预期的结果。
我通过按如下方式输入代码来解决它。
按如下方式替换任务。
public var task: Task {
switch self {
case .getInfo(contactID: _, types: let types):
var arrayOfTypesInString = types.joined(separator: "", "")
arrayOfTypesInString = ""(arrayOfTypesInString)""
let params = ["types": "[(arrayOfTypesInString)]"]
return .requestParameters(
parameters: params,
encoding: URLEncoding.queryString
)
}
}
}
现在,我已经完成了手动JSON
编码。另一种方法是,首先将数据转换为JSON
和从JSON
创建字符串和供应。
我不知道MOYA
我有类似的问题和类似的结构,我已经解决了这个问题
public protocol Router:URLRequestConvertible {
var endPoint : String {get}
var parameters:Parameters? {get}
var httpMethod : HTTPMethod {get}
var encoding:ParameterEncoding {get}
func asURLRequest() throws -> URLRequest
}
现在我有了从包含的所有协议属性构建 URL 的扩展encoding:ParameterEncoding
extension Router {
func asURLRequest() throws -> URLRequest {
// Generate URL
let url = try Constants.APIConstants.BaseURL.production.rawValue.asURL()
// Generate URL Request
var urlRequest = URLRequest(url: url.appendingPathComponent(endPoint))
// Generate Headers
urlRequest.addValue("application/json", forHTTPHeaderField:"Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
urlRequest.addValue(Constants.APIConstants.RequestKeys.APIKEY_Val, forHTTPHeaderField: Constants.APIConstants.RequestKeys.APIKEY)
urlRequest.addValue(Constants.APIConstants.RequestKeys.APIVERSION_Val , forHTTPHeaderField:Constants.APIConstants.RequestKeys.APIVERSION )
urlRequest.addValue(authToken, forHTTPHeaderField:Constants.APIConstants.RequestKeys.AUTHTOKEN )
urlRequest.httpMethod = self.httpMethod.rawValue
if let parameters = parameters {
do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options:[])
urlRequest = try self.encoding.encode(urlRequest, with: parameters)
} catch {
throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
}
}
return urlRequest
}
}
现在我有枚举
enum APIRouterUserModule : Router {
case login(email:String,password:String)
// Implement all the Router protocol property (similar to your strct.) here is example for encoding
var encoding: ParameterEncoding {
switch self {
case .login :
return URLEncoding.queryString
//Return any of them
/*
URLEncoding.queryString
URLEncoding.default
URLEncoding.httpBody
JSONEncoding.default
JSONEncoding.prettyPrinted */
}
}
}
希望对您有所帮助