I wanna consume this URLs:
"https://api.github.com/search/repositories?q=language:Swift&sort=stars&page={param}"
"https://api.github.com/repos/{param}/{param}/pulls"
In the first URL the language and classification parameters ("Swift", "stars") are constant, so I set the values directly in the path, should i try to define the values in the parameters attribute? And try to send "Swift" and "stars" as constants values? I can mantain the structure in the second URL?
Currently the request URL is changed to: "https://api.github.com/search/repositories%3Fq=language:Swift&sort=stars&page=1". After i call the provider.
import Moya
import Alamofire
import Foundation
enum GithubAPI{
case repository(page: Int)
case pullRequest(repository: Repository)
}
extension GithubAPI: TargetType{
/// The method used for parameter encoding.
var baseURL: URL { return
URL(string: "https://api.github.com/")!
}
/// The path to be appended to `baseURL` to form the full `URL`.
public var path: String {
switch self {
case .repository(let page): return "search/repositories?q=language:Swift&sort=stars&page=\(page)"
case .pullRequest(let repository): return "repos/\(repository.owner.name)/\(repository.name)/pulls"
}
}
var method: Moya.Method{
switch self{ case .repository, .pullRequest : return .get}
}
// I tried use JSONEncoding too
public var parameterEncoding: Moya.ParameterEncoding{ return URLEncoding.default}
public var parameters: [String: Any]?{
switch self{
case .repository : return nil
case .pullRequest : return nil
}
}
public var task: Task{ return .request}
public var sampleData: Data{
switch self{
default : return Data()
}
}
I think you will have better luck if you make the language and sort query parameters constants in the parameters return as opposed to hard coding them in the path. That way they don't escaped as you are seeing. Let us know if that works for you!
So, i resolved using the endPoint constructor
let urlSolver = { (target: GithubAPI) -> Endpoint<GithubAPI> in
let url = target.baseURL.absoluteString.appending(target.path)
return Endpoint(url: url, sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
}
let rxProvider = RxMoyaProvider<GithubAPI>(endpointClosure: urlSolver)
Hi, I have a problem similar to this. In iOS 9.3, ":" is not changed to "%3A" but to "."
I tried using URLEncoding.queryString but no luck. Any solution?
Can you share your code where this is happening?
@YkmLo, that is the code I used to solve this problem one year ago.
GithubAPI:
public var path: String {
switch self {
case .repository(let page): return "search/repositories?q=language:Swift&sort=stars&page=\(page)"
case .pullRequest(let repository):return "repos/\(repository.owner.login)/\(repository.name)/pulls"
case .user(let username) : return "users/\(username)"
case .prIssues(let repository, let state) : return "search/issues?q=repo:\(repository.owner.login)/\(repository.name)+is:pr+is:\(state)"
}
}
public var parameterEncoding: Moya.ParameterEncoding{ return URLEncoding.default }
GithubAPIManager:
let urlSolver = { (target: GithubAPI) -> Endpoint<GithubAPI> in
let url = target.baseURL.absoluteString.appending(target.path)
return Endpoint(url: url, sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
}
I used a different URLEncoding but I don't think that is the reason. The urlSolver will override the default endpoint builder method, I suppose that default urlSolver parses : to . . I think if you debug this method you will find an answer.
Most helpful comment
So, i resolved using the endPoint constructor