I am a newbie to Moya, and I knew that the Moya is the most populate repo that integrate Alamofire and ReactiveCocoa.
I have two question.
public enum GitHub {
case Zen
case UserProfile(String)
case UserRepositories(String)
}
sometimes, I need pass multiple parameters for specific API. For example the UserProfile, I need pass two parameters String, Int. How can I do this?
In the demo, the parameters and paths are mixed together in the GitHub, and this will cause the GitHubProvider more and more complex.
extension GitHub: TargetType {
public var baseURL: NSURL { return NSURL(string: "https://api.github.com")! }
public var path: String {
switch self {
case .Zen:
return "/zen"
case .UserProfile(let name):
return "/users/\(name.URLEscapedString)"
case .UserRepositories(let name):
return "/users/\(name.URLEscapedString)/repos"
}
}
public var method: Moya.Method {
return .GET
}
public var parameters: [String: AnyObject]? {
switch self {
case .UserRepositories(_):
return ["sort": "pushed"]
default:
return nil
}
}
public var sampleData: NSData {
switch self {
case .Zen:
return "Half measures are as bad as nothing at all.".dataUsingEncoding(NSUTF8StringEncoding)!
case .UserProfile(let name):
return "{\"login\": \"\(name)\", \"id\": 100}".dataUsingEncoding(NSUTF8StringEncoding)!
case .UserRepositories(_):
return "[{\"name\": \"Repo Name\"}]".dataUsingEncoding(NSUTF8StringEncoding)!
}
}
}
And I want to know that can I split the parameters or paths for each model, like the octokit.objc?
refer to this article Basic
enum MyService {
case Zen
case ShowUser(id: Int)
case CreateUser(firstName: String, lastName: String)
}
You can split it into more providers(TargetType). But it just works fine in this way.
Does this works for you?
For question 2, there's also a struct-based solution we added here, but haven't had time to document yet.
@holysin is this issue resolved to your satisfaction?
Most helpful comment
question 1
refer to this article Basic
question 2
You can split it into more providers(TargetType). But it just works fine in this way.
Does this works for you?