Moya: How to implement multi parameters separated with comma

Created on 27 Apr 2016  路  15Comments  路  Source: Moya/Moya

Hi,
I would like to know how can I implement this with Moya as I have no idea how to send multiple parameter separated with comma.

curl -X GET "https://api.spotify.com/v1/albums/?ids=41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4,6UXCm6bOO4gFlDQZV5yL37"

Thanks

question

All 15 comments

Hmm, good question. Generally, parameter encoding doesn't work like that (as far as I know). You'd more typically see ids sent as an array, like ids[]=41MnTivkwTO3UUJ8DrqEJJ&ids[]=6JWc4iAiJ9FjyK0B59ABb4&ids[]=6UXCm6bOO4gFlDQZV5yL37, but I admit it's been a while. Let's see how we would do what you're looking for.

This is how I would accomplish this; add an associated value to the Album enum case that holds a String array of the ideas:

case Albums(ids: [String])

Then, in the parameters implementation, do the following:

var parameters: [String: AnyObject]? {
  switch self {
    ...
    case .Albums(let ids):
      return ["ids": (ids as NSArray).componentsJoinedByString(",")]
    ...
  }
}

And that should do it. Let us know how it goes!

Hi,
Thanks for you help.
Seams that case Albums(ids: [String]) doesn't works.
image

@Miasma87 This is an error with your swift code:

case .getSeveralAlbums(let ids):  // remove ': [String]' from your code

And it looks like you're in the wrong section - you should be modifying the var parameters, not the URL path.

@colinta Yeah but I will not be able anymore to pass Array, right?

@colinta But the array is passed through the path

Sure you can - the list of ids is assigned to the enum as an associated value.

The path is not supposed to include the ?- GET parameters. They will be added to the URL _for_ you, when you return them from the parameters property. Don't try to construct the entire URL yourself (like you might in JavaScript).

enum API {
  case .Status
  case .Albums([String])
}

var parameters: [String: AnyObject]? {
  switch self {
  case .Status: return nil
  case .Albums(let ids):
    return ["ids": (ids as NSArray).componentsJoinedByString(",")]
  }
}

These solutions end up encoding the commas a %2C which isn't ideal.

@Miasma87 has this issue been addressed to your satisfaction?

Hi, Sorry for the delay, I came back to this issue but I switch to Swift 3.
So now I'm stuck with a Cannot call value of non-function type '((String) -> String)!' On .componentsJoined(",")
Any idea?
:/

Ok, I found it, it's:
(embedded as AnyObject).componentsJoined(by: ",")
Let me check that it works correctly and I come back here.

It seems to work :)
Thanks guys!
@aehlke Yeah I have the %2C even adding removingPercentEncoding to (embedded as AnyObject).componentsJoined(by: ",") does not works :/

@VivienGiraud glad you found out the issue! Also looking by code, if you are using Swift's Structs API for arrays (instead of NSArray), you don't have to cast it and you can just use joinWithSeparator(_:) (in Swift 2) or joined(separator:) (in Swift 3) method:

["test", "test2"].joined(separator: ",")

Yeah, that's what I did :)
Thanks!

Hi ,
I am facing same issue

// MARK: - Parameters
var parameters: Parameters? {
var params = Parameters()
switch self {
case .jobList(let page, let pageSize, let jobTypes):
params["page"] = page
params ["statuses"] = jobTypes.joined(separator: ",")
params["page_size"] = pageSize
}
return params
}

It doesn't work.

Was this page helpful?
0 / 5 - 0 ratings