Moya v9
Swift 4
Xcode v9
The following is my test code for Moya. I'm trying to access an api from a server which have its API in these two format
https://www.api.example.com/3/article/ARTICLE_ID/?api_key=12345
https://www.api.example.com/3/search/mult/?api_key=12345&query=world
Here is my target
enum API {
case SearchMulti(SearchText: String)
case getArticleByID(ArticleID: String)
}
extension API: TargetType {
var baseURL: URL {
return getWSURLValue()
}
func getWSURLValue() -> URL {
// I'm getting the url of the ws from firebase using remote config
let temp: String = Facade.getInstance().remoteConfig["WSURL"].stringValue!
return URL(string: temp)!
}
func getAPIValue() -> String {
// I'm getting the url of the api key from firebase using remote config
return Facade.getInstance().remoteConfig["API"].stringValue!
}
var path: String {
switch self {
case .SearchMulti(let SearchText):
return "search/mult/"
//+ "?api_key=" + getAPIValue()
case .getArticleByID(let ArticleID):
return "article/" + ArticleID
//+ "?api_key=" + getAPIValue()
}
}
var method: Moya.Method {
switch self {
case .SearchMulti,
.getArticleByID:
return .get
}
}
var parameters: [String: Any]? {
switch self {
case .SearchMulti(let SearchText):
return ["api_key": getAPIValue(), "query": SearchText]
case .getArticleByID(let ArticleID):
return ["api_key": getAPIValue()]
default:
return ["api_key": getAPIValue()]
}
}
var sampleData: Data {
return Data()
}
var task: Task {
return .requestPlain
}
var headers: [String : String]? {
return nil
}
}
Here is my provider
let provider = MoyaProvider<API>(plugins: [NetworkLoggerPlugin(verbose: true)])
provider.request(.getArticleByID(ArticleID: self.SaerchTextBox.text ?? "")) { result in
switch result {
case let .success(moyaResponse):
let statusCode = moyaResponse.statusCode
if (statusCode == 200) {
print("API Request Succeeded")
let data = moyaResponse.data
} else {
print("API Request Failed: " + String(statusCode))
}
case let .failure(error):
print("API Request Failed\nError: ")
print(error)
}
}
The request according to the log never send the parameters
he only send request to the baseURL + path without the parameters
am I doing something wrong here
Hey @hamada147. When switching to Moya 9.* from 8.* you need to follow our migrate guide as few changes were made to parameters/encoding/task. Basically there is no standalone parameters/encoding variables, everything is defined in the task property, and you in yours specify requestPlain which suggest that you do not send the parameters in your request at all. More in the docs I linked.
Let me know if something in the docs is not clear so we can improve it if that happens.
I see. No your documentation is really good. It was my mistake for not checking the release notes before using the new version.
Oh, dob鈥檛 worry @hamada147, happens all the time. Glad it helped! 馃帀
Most helpful comment
Hey @hamada147. When switching to Moya 9.* from 8.* you need to follow our migrate guide as few changes were made to parameters/encoding/task. Basically there is no standalone parameters/encoding variables, everything is defined in the
taskproperty, and you in yours specifyrequestPlainwhich suggest that you do not send the parameters in your request at all. More in the docs I linked.Let me know if something in the docs is not clear so we can improve it if that happens.