Moya: How to implement multiple parameters, and can I split the solution for each model

Created on 12 May 2016  路  3Comments  路  Source: Moya/Moya

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.

question 1

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?

question 2

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?

question

Most helpful comment

question 1

refer to this article Basic

enum MyService {
    case Zen
    case ShowUser(id: Int)
    case CreateUser(firstName: String, lastName: String)
}

question 2

You can split it into more providers(TargetType). But it just works fine in this way.

Does this works for you?

All 3 comments

question 1

refer to this article Basic

enum MyService {
    case Zen
    case ShowUser(id: Int)
    case CreateUser(firstName: String, lastName: String)
}

question 2

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamwysoc picture kamwysoc  路  3Comments

ghost picture ghost  路  3Comments

pedrovereza picture pedrovereza  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments

Ravichandrane picture Ravichandrane  路  4Comments