Create a gist now

Instantly share code, notes, and snippets.

@kean /moya.swift Secret
Created Jul 1, 2017

What would you like to do?
// MARK: - TargetType Protocol Implementation
extension MyService: TargetType {
var baseURL: URL { return URL(string: "https://api.myservice.com")! }
var path: String {
switch self {
case .zen:
return "/zen"
case .showUser(let id), .updateUser(let id, _, _):
return "/users/\(id)"
case .createUser(_, _):
return "/users"
case .showAccounts:
return "/accounts"
}
}
var method: Moya.Method {
switch self {
case .zen, .showUser, .showAccounts:
return .get
case .createUser, .updateUser:
return .post
}
}
var parameters: [String: Any]? {
switch self {
case .zen, .showUser, .showAccounts:
return nil
case .createUser(let firstName, let lastName), .updateUser(_, let firstName, let lastName):
return ["first_name": firstName, "last_name": lastName]
}
}
var parameterEncoding: ParameterEncoding {
switch self {
case .zen, .showUser, .showAccounts:
return URLEncoding.default // Send parameters in URL for GET, DELETE and HEAD. For other HTTP methods, parameters will be sent in request body
case .updateUser:
return URLEncoding.queryString // Always sends parameters in URL, regardless of which HTTP method is used
case .createUser:
return JSONEncoding.default // Send parameters as JSON in request body
}
}
var sampleData: Data {
switch self {
case .zen:
return "Half measures are as bad as nothing at all.".utf8Encoded
case .showUser(let id):
return "{\"id\": \(id), \"first_name\": \"Harry\", \"last_name\": \"Potter\"}".utf8Encoded
case .createUser(let firstName, let lastName):
return "{\"id\": 100, \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
case .updateUser(let id, let firstName, let lastName):
return "{\"id\": \(id), \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
case .showAccounts:
// Provided you have a file named accounts.json in your bundle.
guard let url = Bundle.main.url(forResource: "accounts", withExtension: "json"),
let data = try? Data(contentsOf: url) else {
return Data()
}
return data
}
}
var task: Task {
switch self {
case .zen, .showUser, .createUser, .updateUser, .showAccounts:
return .request
}
}
}
// MARK: - Helpers
private extension String {
var urlEscaped: String {
return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
}
var utf8Encoded: Data {
return self.data(using: .utf8)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment