mastodon-ios/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+App.swift

118 lines
3.8 KiB
Swift
Raw Normal View History

2021-01-26 10:38:30 +01:00
//
// Mastodon+API+App.swift
//
//
// Created by xiaojian sun on 2021/1/25.
//
import Foundation
2021-01-27 09:01:20 +01:00
import Combine
2021-01-26 10:38:30 +01:00
2021-01-27 09:01:20 +01:00
extension Mastodon.API.App {
2021-01-27 07:50:13 +01:00
2021-01-26 11:11:44 +01:00
static func appEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("apps")
}
2021-01-27 09:01:20 +01:00
2021-01-29 12:38:11 +01:00
static func verifyCredentialsEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("apps/verify_credentials")
}
/// Create an application
///
/// Using this endpoint to obtain `client_id` and `client_secret` for later OAuth token exchange
///
/// - Since: 0.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/1/29
/// # Reference
/// [Document](https://docs.joinmastodon.org/methods/apps/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `CreateQuery`
/// - Returns: `AnyPublisher` contains `Application` nested in the response
2021-01-27 09:01:20 +01:00
public static func create(
session: URLSession,
2021-01-27 11:46:14 +01:00
domain: String,
2021-01-27 09:01:20 +01:00
query: CreateQuery
2021-01-28 07:52:35 +01:00
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Application>, Error> {
2021-01-29 12:38:11 +01:00
let request = Mastodon.API.post(
2021-01-27 11:46:14 +01:00
url: appEndpointURL(domain: domain),
query: query,
authorization: nil
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
2021-01-28 07:52:35 +01:00
let value = try Mastodon.API.decode(type: Mastodon.Entity.Application.self, from: data, response: response)
2021-01-27 11:46:14 +01:00
return Mastodon.Response.Content(value: value, response: response)
2021-01-29 12:38:11 +01:00
}
.eraseToAnyPublisher()
}
/// Verify application token
///
/// Using this endpoint to verify App token
///
/// - Since: 2.0.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/1/29
/// # Reference
/// [Document](https://docs.joinmastodon.org/methods/apps/)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - authorization: App token
/// - Returns: `AnyPublisher` contains `Application` nested in the response
public static func verifyCredentials(
session: URLSession,
domain: String,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Application>, Error> {
let request = Mastodon.API.get(
url: verifyCredentialsEndpointURL(domain: domain),
query: nil,
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: Mastodon.Entity.Application.self, from: data, response: response)
return Mastodon.Response.Content(value: value, response: response)
2021-01-27 11:46:14 +01:00
}
.eraseToAnyPublisher()
2021-01-27 09:01:20 +01:00
}
2021-01-27 07:50:13 +01:00
}
extension Mastodon.API.App {
2021-01-26 11:11:44 +01:00
2021-01-27 11:46:14 +01:00
public struct CreateQuery: Codable, PostQuery {
2021-01-26 11:11:44 +01:00
public let clientName: String
public let redirectURIs: String
public let scopes: String?
public let website: String?
2021-01-26 10:38:30 +01:00
2021-01-27 11:46:14 +01:00
enum CodingKeys: String, CodingKey {
case clientName = "client_name"
case redirectURIs = "redirect_uris"
case scopes
case website
}
2021-01-27 09:01:20 +01:00
public init(
clientName: String,
redirectURIs: String,
2021-01-27 09:01:20 +01:00
scopes: String? = "read write follow push",
website: String?
) {
2021-01-26 11:11:44 +01:00
self.clientName = clientName
self.redirectURIs = redirectURIs
2021-01-26 10:38:30 +01:00
self.scopes = scopes
self.website = website
}
}
2021-01-26 11:11:44 +01:00
2021-01-26 10:38:30 +01:00
}