2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
2020-08-31 01:40:58 +00:00
|
|
|
import HTTP
|
2020-09-04 01:55:46 +00:00
|
|
|
import Mastodon
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-30 23:33:11 +00:00
|
|
|
public enum AccessTokenEndpoint {
|
2020-07-29 23:50:30 +00:00
|
|
|
case oauthToken(
|
2020-09-11 09:55:06 +00:00
|
|
|
clientID: String,
|
|
|
|
clientSecret: String,
|
|
|
|
grantType: String,
|
|
|
|
scopes: String,
|
|
|
|
code: String?,
|
|
|
|
redirectURI: String?
|
|
|
|
)
|
2020-09-12 02:50:42 +00:00
|
|
|
case accounts(Registration)
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension AccessTokenEndpoint {
|
|
|
|
struct Registration {
|
|
|
|
public var username = ""
|
|
|
|
public var email = ""
|
|
|
|
public var password = ""
|
|
|
|
public var locale = "en"
|
|
|
|
public var reason = ""
|
|
|
|
public var agreement = false
|
|
|
|
|
|
|
|
public init() {}
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 23:59:49 +00:00
|
|
|
extension AccessTokenEndpoint: Endpoint {
|
2020-08-30 23:33:11 +00:00
|
|
|
public typealias ResultType = AccessToken
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-09-11 09:55:06 +00:00
|
|
|
public var context: [String] {
|
|
|
|
switch self {
|
|
|
|
case .oauthToken:
|
|
|
|
return []
|
|
|
|
case .accounts:
|
|
|
|
return defaultContext
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-30 23:33:11 +00:00
|
|
|
public var pathComponentsInContext: [String] {
|
2020-09-11 09:55:06 +00:00
|
|
|
switch self {
|
|
|
|
case .oauthToken:
|
|
|
|
return ["oauth", "token"]
|
|
|
|
case .accounts:
|
|
|
|
return ["accounts"]
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 23:33:11 +00:00
|
|
|
public var method: HTTPMethod {
|
2020-07-29 23:50:30 +00:00
|
|
|
switch self {
|
2020-09-11 09:55:06 +00:00
|
|
|
case .oauthToken, .accounts: return .post
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 07:04:37 +00:00
|
|
|
public var jsonBody: [String: Any]? {
|
2020-07-29 23:50:30 +00:00
|
|
|
switch self {
|
2020-09-11 09:55:06 +00:00
|
|
|
case let .oauthToken(clientID, clientSecret, grantType, scopes, code, redirectURI):
|
|
|
|
var params = [
|
2020-07-29 23:50:30 +00:00
|
|
|
"client_id": clientID,
|
|
|
|
"client_secret": clientSecret,
|
|
|
|
"grant_type": grantType,
|
2020-09-11 09:55:06 +00:00
|
|
|
"scope": scopes]
|
|
|
|
|
|
|
|
params["code"] = code
|
|
|
|
params["redirect_uri"] = redirectURI
|
|
|
|
|
|
|
|
return params
|
2020-09-12 02:50:42 +00:00
|
|
|
case let .accounts(registration):
|
2020-09-11 09:55:06 +00:00
|
|
|
var params: [String: Any] = [
|
2020-09-12 02:50:42 +00:00
|
|
|
"username": registration.username,
|
|
|
|
"email": registration.email,
|
|
|
|
"password": registration.password,
|
|
|
|
"locale": registration.locale,
|
|
|
|
"agreement": registration.agreement]
|
2020-09-11 09:55:06 +00:00
|
|
|
|
2020-09-12 02:50:42 +00:00
|
|
|
if !registration.reason.isEmpty {
|
|
|
|
params["reason"] = registration.reason
|
|
|
|
}
|
2020-09-11 09:55:06 +00:00
|
|
|
|
|
|
|
return params
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|