Skip to content

Instantly share code, notes, and snippets.

@bdbergeron
Last active August 29, 2015 14:22
Show Gist options
  • Save bdbergeron/e4d0090b4542f380780b to your computer and use it in GitHub Desktop.
Save bdbergeron/e4d0090b4542f380780b to your computer and use it in GitHub Desktop.
BDBOAuth1Manager POST with JSON payload
//
// DiscogsSessionManager.swift
//
// Copyright (c) 2015 Bradley David Bergeron
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Foundation
import BDBOAuth1Manager
class DiscogsAPIClient {
let networkManager: BDBOAuth1SessionManager
init(consumerKey: String, secret: String) {
var baseURL = NSURL(string: "https://api.discogs.com/")
self.networkManager = BDBOAuth1SessionManager(baseURL: baseURL, consumerKey: consumerKey, consumerSecret: secret)
}
func createListing (parameters: [String : AnyObject], completion: (listingInfo: AnyObject!, error: NSError!) -> Void) -> NSURLSessionDataTask! {
return self.networkManager.POSTJSON("/marketplace/listings",
parameters: parameters,
success: { (task, responseObject) -> Void in
completion(listingInfo: responseObject, error: nil)
},
failure: { (task, error) -> Void in
completion(listingInfo: nil, error: error)
}
)
}
func deleteListing (listingID: Int, completion: (error: NSError!) -> Void) -> NSURLSessionDataTask! {
return self.networkManager.DELETE("/marketplace/listings/\(listingID)",
parameters: nil,
success: { (task, responseObject) -> Void in
completion(error: nil)
},
failure: { (task, error) -> Void in
completion(error: error)
}
)
}
}
//
// DiscogsSessionManager.swift
//
// Copyright (c) 2015 Bradley David Bergeron
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Foundation
import BDBOAuth1Manager
class DiscogsSessionManager: BDBOAuth1SessionManager {
func POSTJSON(URLString: String!, parameters: AnyObject!, success: ((NSURLSessionDataTask!, AnyObject!) -> Void)!, failure: ((NSURLSessionDataTask!, NSError!) -> Void)!) -> NSURLSessionDataTask! {
var serializationError: NSError?
let request = self.requestSerializer.requestWithMethod("POST",
URLString: NSURL(string: URLString, relativeToURL: self.baseURL)?.absoluteString,
parameters: nil,
error: &serializationError)
if let serializationError = serializationError {
if let failure = failure {
dispatch_async(self.completionQueue ?? dispatch_get_main_queue()) {
failure(nil, serializationError)
}
}
return nil
}
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions.allZeros, error: &serializationError)
if let serializationError = serializationError {
if let failure = failure {
dispatch_async(self.completionQueue ?? dispatch_get_main_queue()) {
failure(nil, serializationError)
}
}
return nil
}
var task: NSURLSessionDataTask!
task = self.dataTaskWithRequest(request, completionHandler: { (response, responseObject, error) -> Void in
if let error = error {
if let failure = failure {
failure(task, error)
}
} else {
if let success = success {
success(task, responseObject)
}
}
})
task.resume()
return task
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment