Skip to content

Instantly share code, notes, and snippets.

@GE-N
Last active August 29, 2015 14:25
Show Gist options
  • Save GE-N/01b6d38cb7e570722478 to your computer and use it in GitHub Desktop.
Save GE-N/01b6d38cb7e570722478 to your computer and use it in GitHub Desktop.
Sample HTTPRequest with Swift's Enum as parameter
/**
Host: Staging => http://staging.myblog.com/api, Production => https://myblog.com/api
Method: /blog
Read blog — GET /blog/:id
Create blog — POST /blog, body: title=<string>&body=<string>
Update blog — UPDATE /blog/:id, body: title=<string>&body=<string>
Delete blog — DELETE /blog:id
*/
enum BlogMethod {
case CREATE(title: string, body: string)
case READ(id: int)
case UPDATE(id: int, title: string, body: string)
case DELETE(id: int)
}
func blog (method: BlogMethod) -> () {
var httpMethod: Alamofire.Method
var body: [String : String]?
var apiPath = “\(API.host.rawValue)/blog”
switch method {
case .CREATE(title, body):
httpMethod = .POST
body = [ “title” = title, “body” = body ]
case .READ(id):
httpMethod = .GET
apiPath += id
case .UPDATE(id, title, body):
httpMethod = .UPDATE
apiPath += id
body = [ “title” = title, “body” = body ]
case .DELETE(id):
httpMethod = .DELETE
apiPath += id
}
Alamofire.request(httpMethod, apiPath, parameters: body)
.responseJSON(_, _, _, _) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment