Swift client for the Discogs API.
This library is still in development.
You can install DGDiscogsClient via CocoaPods by adding to your Podfile:
pod 'OAuthSwiftAlamofire'
pod 'DGDiscogsClient', '0.0.1'
You can install DGDiscogsClient
via the Swift Package Manager by adding to your Package.swift
file:
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.Package(url: "https://github.com/WunDaii/DGDiscogsClient.git", version: “0.0.1”,
]
)
You must authorise the user with Discogs by using the auth flow provided by OAuthSwiftAlamofire.
- Create an
OAuth1Swift
object with your credentials:
oauthSwift = OAuth1Swift(
consumerKey: "YOUR_CONSUMER_KEY”,
consumerSecret: “YOUR_CONSUMER_SECRET”,
requestTokenUrl: "https://api.discogs.com/oauth/request_token",
authorizeUrl: "https://discogs.com/oauth/authorize",
accessTokenUrl: "https://api.discogs.com/oauth/access_token"
)
- Add the
RequestAdapter
toDGDiscogsManager
:
DGDiscogsManager.sharedInstance.adapter = oauthSwift.requestAdapter
let sessionManager = SessionManager.default
sessionManager.adapter = oauthSwift.requestAdapter
- Authorize with
OAuthSwift
:
let _ = oauthSwift.authorize(
withCallbackURL: URL(string: "recordshelf://oauth-callback/discogs")!,
success: { credential, response, parameters in
print(credential.oauthToken)
print(credential.oauthTokenSecret)
UserDefaults.standard.set(credential.oauthToken, forKey: "oauth_token")
UserDefaults.standard.set(credential.oauthTokenSecret, forKey: "oauth_token_secret")
self.load()
},
failure: { error in
print(error.localizedDescription)
})
}
Each request on a DGDiscogs
object returns an enum
called 'result', that has two possible values: success
and failure
. The success
value will pass the expected values from the request, whilst the failure
value typically returns an NSError
.
DGDiscogsManager.sharedInstance.getAuthenticatedUser { (result) in
switch result {
case .success():
print("Successful")
break
case .failure(error: let error):
print("There was an error: \(error?.localizedDescription)")
break
default:
break
}
}
let user = DGDiscogsManager.sharedInstance.user
DGDiscogsManager.sharedInstance.user.getOrders { (result) in
switch result {
case .success(pagination: _, orders: let orders):
guard
let orders = orders
else { return }
print("The user has made \(orders.count) orders.")
break
case .failure(error: let error):
print("There was an error: \(error?.localizedDescription)")
break
default:
break
}
}
- You may compare
DGDiscogsItems
using the==
operator, as this will compare eachitem
'sdiscogsID
anditemType
values. - In requests where pagination is possible, you may omit the
pagination:
parameter when calling the method. This will default to aDGDiscogsUtils.Pagination
with 20 items per page for page 1.