Moya: Missing case in Task enum

In one of my projects, I needed to use the parameters in the query string, and also use the body. Due to the fact that the property path can not be represented as:

public var path: String {
        switch self {
case .myCase(let name): return "api/endpoint?username=\(name)"
}

Here’s what my case should look like:

case requestJSONEncodable(Encodable, parameters: [String: Any], encoding: ParameterEncoding)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@ashfurrow @SD10 @sunshinejr I need case for Encodable body and parameters as queryString. Because “api/endpoint?username=(name)” in path property not working due to encoding. And even if it worked, the solution is very bad, i think.

    case requestJSONEncodable(Encodable)
    case requestParameters(parameters: [String: Any], encoding: ParameterEncoding)
    case requestCompositeData(bodyData: Data, urlParameters: [String: Any])
    case requestCompositeParameters(bodyParameters: [String: Any], bodyEncoding: ParameterEncoding, urlParameters: [String: Any])

None of them allow us to use Encodable + [String: Any] as url parameters. Sure i can use:

    public var task: Task {
        switch self {
        case let .setGlucoseThresholds(_, glucoseThresholds):
            let encoded = try! JSONEncoder().encode(glucoseThresholds)
            return .requestCompositeData(bodyData: encoded, urlParameters: parameters)
        }
    }

But I do not think this is a good decision. Because, this is not the place where we need to encode Encodable to data.

Update my proposal case:

case requestJSONEncodable(Encodable, urlParameters: [String: Any])

@SD10 There is no need to apologize! We can work through these things together as they come up – and things like this do come up, for every project.

Could this be something that Parameter Encoding solves? We have docs here: https://github.com/Moya/Moya/blob/master/docs/Examples/ParameterEncoding.md Basically you can provide a custom bit of code that translates the parameters of the endpoint into the request. Again, a bit of a clutch, but as pointed out this is an uncommon scenario.

Moya should make common things easy and uncommon things possible, and a “clutch” in this case might be worth it for the sake of a smaller API surface area.