I have some global public parameters need to set ,where and how to make it? thanks
Hey @Goallying, you mean you want to add a parameter to every network request in your app? In that case you could use endpointClosure if you want to check/add based on the TargetType or requestClosure if you just want to add it to the URLRequest before firing.
@sunshinejr , I cant get parameters like"target. parameters" , how to add parameters?
This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
@Goallying if you want to use endpointClosure you will need to switch by task, because e.g. in upload the parameters are only available in multipart body, or sometimes you have parameters only in url/body.
Please take a look at https://github.com/Moya/Moya/issues/1793#issuecomment-462242140, it should help!
@sunshinejr I tried to do as you wrote, but it seems not working:
`private var provider = MoyaProvider<MultiTarget>(endpointClosure: { (target: TargetType) -> Endpoint in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: MultiTarget(target))
switch defaultEndpoint.task {
case .requestParameters(var params, _):
params["lng"] = "EN" // this parameter should be passed to every request
default:
break
}
return defaultEndpoint
}, callbackQueue: DispatchQueue.global(qos: .utility))`
I use above provider as following:
provider.request(MultiTarget(someTarget)) { response in
}
Anyway, my default parameters is not sending. It seems I don't use endpointClosure properly. What can be the problem?
@abayken you updated the params, but these are a value type and so it only changed on your side, it didn't propagate to the defaultEndpoint. You need to return the newly formed endpoint after you change it, e.g.:
private var provider = MoyaProvider<MultiTarget>(endpointClosure: { (target: TargetType) -> Endpoint in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: MultiTarget(target))
switch defaultEndpoint.task {
case .requestParameters(var params, let encoding):
params["lng"] = "EN" // this parameter should be passed to every request
return .requestParameters(params, encoding) // <- this is important
default:
break
}
return defaultEndpoint
}, callbackQueue: DispatchQueue.global(qos: .utility))`
@sunshinejr Thanks, it was stupid mistake. endpointClosure should return Endpoint, but in above example you also return Task in return .requestParameters(params, encoding) line. How is that possible?
UPDATE:
It seems the code should be following:
private var provider = MoyaProvider<MultiTarget>(endpointClosure: { (target: TargetType) -> Endpoint in
let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: MultiTarget(target))
switch defaultEndpoint.task {
case .requestParameters(var params, let encoding):
params["lng"] = "EN"
return Endpoint(url: defaultEndpoint.url, sampleResponseClosure: defaultEndpoint.sampleResponseClosure, method: defaultEndpoint.method, task: .requestParameters(parameters: params, encoding: encoding), httpHeaderFields: defaultEndpoint.httpHeaderFields)
default:
break
}
return defaultEndpoint
}, callbackQueue: DispatchQueue.global(qos: .utility))`
Let us know if it is best way of solving the problem. Thanks!
@abayken yeah sorry! you鈥檙e correct and you need to return a proper Endpoint, not just a task, but great that you figured it out!
And yeah for now it鈥檚 how you have to approach this, it might be better in the future but right now it is what it is.
great锛乼hanks
no problem at all, glad you all were able to resolve the issue - in that case I'm closing it but please don't hesitate to reopen in case there is still something that we could help you with!
Most helpful comment
@abayken yeah sorry! you鈥檙e correct and you need to return a proper
Endpoint, not just a task, but great that you figured it out!And yeah for now it鈥檚 how you have to approach this, it might be better in the future but right now it is what it is.