Construct Moya TargetType in iOS

2019-09-12 12:17发布

I'm trying to use Moya with RxSwift in my project

I'm facing the problem with url contain "?"

This TargetType I have create

private extension String {
    var URLEscapedString: String {
        return self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)!
    }
}

enum TMDb {
    case discoverMovieList(page: Int)
    case discoverMovieDetail(moive: Movie)
}

extension TMDb: TargetType {
    var baseURL: URL { return URL(string: BASE_URL)! }

    var path: String {
        switch self {
        case .discoverMovieList(page: let page):
            return "discover/movie?api_key=\(API_KEY)&sort_by=release_date.desc&page=\(page)"
        }
    }
    var method: Moya.Method {
        return .get
    }
    var parameters: [String: Any]? {
        return nil
    }
    var sampleData: Data {
        switch self {
            case .discoverMovieList(page: _):
                return "test".data(using: .utf8)!
            case .discoverMovieDetail(moive: _):
                return "test1".data(using: .utf8)!
        }
    }
    var task: Task {
        return .request
    }
    var parameterEncoding: ParameterEncoding {
        return URLEncoding.default
    }

}

The problem is when I make the request. The url path return is somehow not correct

This is the url i got from console

Optional("https://api.themoviedb.org/3/discover/movie%3Fapi_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1")

But the correct one should be

https://api.themoviedb.org/3/discover/movie?api_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1

There something wrong when handle the "?" (it become %3F" character in the url. How can we make it work normally?

Update

This is how I call my Moya

let provider: RxMoyaProvider<TMDb>
    let persistentContainer: PersistentContainer

    func discoverMoiveList(for page: Int) {
        self.provider.request(TMDb.discoverMovieList(page: 1)) { (result) in
            print(result.value?.request?.url?.absoluteString ?? "no url")

        }
    }
}

标签: ios url moya
3条回答
Evening l夕情丶
2楼-- · 2019-09-12 12:37

The problem is not about "?". It occurred because of you used optional values in your path URL. Control BASE_URL and API_KEY, they must not be optional values.

There is no problem with the Moya. Here Moya-Logger result for your provider:

Moya_Logger: [29/04/2017 15:48:22] Request: https://api.themoviedb.org/3/discover/movie%3Fapi_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1
Moya_Logger: [29/04/2017 15:48:22] Request Headers: [:]
Moya_Logger: [29/04/2017 15:48:22] HTTP Request Method: GET
Moya_Logger: [29/04/2017 15:48:23] Response: <NSHTTPURLResponse: 0x618000220360> { URL: https://api.themoviedb.org/3/discover/movie%3Fapi_key=58da429caf2e25e8ff9436665e2f0e36&sort_by=release_date.desc&page=1 } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 89;
"Content-Type" = "application/octet-stream";
Date = "Sat, 29 Apr 2017 12:48:23 GMT";
Server = openresty;
"X-RateLimit-Limit" = 40;
"X-RateLimit-Remaining" = 39;
"X-RateLimit-Reset" = 1493470113;
} }
查看更多
3楼-- · 2019-09-12 12:51

Here you've got a topic how to get an optional variable without Opt(). I prefer that way where you create an extension:

extension Optional {
    func asStringOrNilText() -> String {
        switch self {
        case .some(let value):
            return String(describing: value)
        case _:
            return "(nil)"
        }
    }

}

print(result.value?.request?.url?.absoluteString.asStringOrNilText())
查看更多
ら.Afraid
4楼-- · 2019-09-12 13:02

You should pass anything after the ? as parameter, this way Moya knows how to construct the URL correctly.

  var path: String {
    switch self {
    case .discoverMovieList(page: let page):
        return "discover/movie"
    }
}
var method: Moya.Method {
    return .get
}
var parameters: [String: Any]? {
     switch self {
     case .discoverMovieList(page: let page):
       return "["api_key":"\(API_KEY)",
                "sort_by":"release_date.desc",
                "page":page]"
}
}
查看更多
登录 后发表回答