简体   繁体   中英

Moya Alamofire request with no url coding

How to consume a get-API (having following request-structure) with Moya?

http://some.environment.com/some/api/some/contacts/81193?types=["details", "permissions"]

Here is what I've tried.

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
    }

}

Above code produces following URL.

http://some.environment.com/some/api/some/contacts/81193?types%5B%5D=details&types%5B%5D=permissions

I've tried followings for encoding

  • URLEncoding.queryString
  • URLEncoding.default
  • URLEncoding.httpBody
  • JSONEncoding.default
  • JSONEncoding.prettyPrinted

None of the encoding helped me to produce expected result.

I solved it by putting code as follows.

Replace task as follows.

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
            )
        }
    }
}

For now, I've done manual JSON encoding. Alternate way of doing it would be, first convert data to JSON & from JSON create string & supply.

I don't know about MOYA

I had similar issue and similar structure what you have and I have fixed that with

public protocol Router:URLRequestConvertible {
    var endPoint : String {get}
    var parameters:Parameters? {get}
    var httpMethod : HTTPMethod {get}
    var encoding:ParameterEncoding {get}
     func asURLRequest() throws -> URLRequest
}

Now I have extension that build the URL from all the protocol properties included 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

    }
}

Now I have enum

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 */
                  }

             }
   }

Hope it is helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM