Skip to content

Instantly share code, notes, and snippets.

@clooth
Created October 8, 2015 14:35
Show Gist options
  • Save clooth/9a74ec2110715c682966 to your computer and use it in GitHub Desktop.
Save clooth/9a74ec2110715c682966 to your computer and use it in GitHub Desktop.
///
/// All targets have the same base URL
///
let BaseURL = NSURL(string: "http://example.com/v1")!
///
/// First target handles user-related api endpoints
///
/// .Authentication needs to:
/// 1. Make the request to the path with the given credentials
/// 2. Get back an access token and store it somewhere
///
/// .Profile requires that access token to be set in the request headers, since it's an authenticated endpoint
/// but I can't seem to figure out how I can do this simply enough.
///
/// Do I need a wrapper class for MoyaProvider that keeps track of all my targets or something and then
/// passes the stored access tokens around to the endpoints and requests?
///
enum FirstTarget: MoyaTarget {
case Authenticate(username: String, password: String)
case Profile
var baseURL: NSURL { return BaseURL }
var path: String {
switch self {
case .Authenticate:
return "auth"
case .Profile:
return "profile"
}
}
var method: Moya.Method {
switch self {
case .Authenticate: return .POST
case .Profile: return .GET
}
}
var parameters: [String: AnyObject]? {
switch self {
case .Authenticate(let username, let password):
return ["username": username, "password": password]
case .Profile:
return nil
}
}
var sampleData: NSData { return NSData() }
}
///
/// The second target handles photo related api calls, all calls require the before mentioned access token as they
/// are authenticated api endpoints.
///
/// In addition, the .Upload needs to construct a custom HTTPBody for the image data to be sent to the upload API.
///
/// All endpoints except .Authenticate need the access token in all targets.
///
enum SecondTarget: MoyaTarget {
case Photos
case Upload(NSData)
var baseURL: NSURL { return BaseURL }
var path: String {
switch self {
case .Photos: return "photos"
case .Upload: return "photos/upload"
}
}
var method: Moya.Method {
switch self {
case .Photos: return .GET
case .Upload: return .POST
}
}
var parameters: [String: AnyObject]? {
return nil
}
var sampleData: NSData { return NSData() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment