Created
January 22, 2018 03:56
-
-
Save NghiaTranUIT/ce8d3031b2e859b4612b5a7312e378f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum GitHub { | |
case zen | |
case userProfile(String) | |
case userRepositories(String) | |
} | |
extension GitHub: TargetType { | |
public var baseURL: URL { return URL(string: "https://api.github.com")! } | |
public var path: String { | |
switch self { | |
case .zen: | |
return "/zen" | |
case .userProfile(let name): | |
return "/users/\(name.urlEscaped)" | |
case .userRepositories(let name): | |
return "/users/\(name.urlEscaped)/repos" | |
} | |
} | |
public var method: Moya.Method { | |
return .get | |
} | |
public var task: Task { | |
switch self { | |
case .userRepositories: | |
return .requestParameters(parameters: ["sort": "pushed"], encoding: URLEncoding.default) | |
default: | |
return .requestPlain | |
} | |
} | |
public var validate: Bool { | |
switch self { | |
case .zen: | |
return true | |
default: | |
return false | |
} | |
} | |
public var sampleData: Data { | |
switch self { | |
case .zen: | |
return "Half measures are as bad as nothing at all.".data(using: String.Encoding.utf8)! | |
case .userProfile(let name): | |
return "{\"login\": \"\(name)\", \"id\": 100}".data(using: String.Encoding.utf8)! | |
case .userRepositories(let name): | |
return "[{\"name\": \"\(name)\"}]".data(using: String.Encoding.utf8)! | |
} | |
} | |
public var headers: [String: String]? { | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment