metatext-app-ios-iphone-ipad/MastodonAPI/Sources/MastodonAPI/Endpoints/AccessTokenEndpoint.swift

90 lines
2.2 KiB
Swift
Raw Normal View History

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