chore: rename and cleanup

This commit is contained in:
CMK 2021-01-26 18:11:44 +08:00
parent 3d5afbd589
commit 65d6ae6e11
7 changed files with 58 additions and 71 deletions

View File

@ -9,9 +9,13 @@ import Combine
import Foundation
public extension Mastodon.API.App {
internal static let appEndpointURL = Mastodon.API.endpointURL.appendingPathComponent("apps")
struct OAuth2Credentials: Codable {
static func appEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("apps")
}
struct Application: Codable {
public let id: String
public let name: String
@ -33,28 +37,33 @@ public extension Mastodon.API.App {
}
}
struct registerAppQuery {
public let client_name: String
public let redirect_uris: String
public let scopes: String
public let website: String
struct CreateAnAppQuery {
public let clientName: String
public let redirectURIs: String
public let scopes: String?
public let website: String?
public init(client_name: String, redirect_uris: String, scopes: String, website: String) {
self.client_name = client_name
self.redirect_uris = redirect_uris
public init(clientName: String, redirectURIs: String, scopes: String?, website: String?) {
self.clientName = clientName
self.redirectURIs = redirectURIs
self.scopes = scopes
self.website = website
}
var queryItems: [URLQueryItem]? {
var items: [URLQueryItem] = []
items.append(URLQueryItem(name: "client_name", value: client_name))
items.append(URLQueryItem(name: "redirect_uris", value: redirect_uris))
items.append(URLQueryItem(name: "scopes", value: scopes))
items.append(URLQueryItem(name: "client_name", value: clientName))
items.append(URLQueryItem(name: "redirect_uris", value: redirectURIs))
scopes.flatMap {
items.append(URLQueryItem(name: "scopes", value: $0))
}
website.flatMap {
items.append(URLQueryItem(name: "website", value: $0))
}
guard !items.isEmpty else { return nil }
return items
}
}
}
extension Mastodon.API.App.OAuth2Credentials: Equatable {}

View File

@ -0,0 +1,22 @@
//
// Mastodon+API+Error.swift
//
//
// Created by MainasuK Cirno on 2021/1/26.
//
import Foundation
extension Mastodon.API.Error {
struct ErrorResponse: Codable {
let error: String
let errorDescription: String?
enum CodingKeys: String, CodingKey {
case error
case errorDescription = "error_description"
}
}
}

View File

@ -9,65 +9,22 @@ import Foundation
import NIOHTTP1
public extension Mastodon.API {
static var baseUrl = ""
static let endpointURL = URL(string: baseUrl + "/api/v1/")!
static func endpointURL(domain: String) -> URL {
return URL(string: "https://" + domain + "/api/v1/")!
}
static let timeoutInterval: TimeInterval = 10
static let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .MastodonStrategy
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
static let httpHeaderDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
return formatter
}()
static let httpHeaderDateFormatter = ISO8601DateFormatter()
enum App {}
}
extension Mastodon.API {
// Error Response when request V1 endpoint
struct ErrorResponse: Codable {
let errors: [ErrorDescription]
struct ErrorDescription: Codable {
public let code: Int
public let message: String
}
}
enum Error { }
enum App { }
// Alternative Error Response when request V1 endpoint
struct ErrorRequestResponse: Codable {
let request: String
let error: String
}
}
extension Mastodon.API {
}
private extension JSONDecoder.DateDecodingStrategy {
static let MastodonStrategy = custom { decoder throws -> Date in
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
let formatterV1 = DateFormatter()
formatterV1.locale = Locale(identifier: "en")
formatterV1.dateFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy"
if let date = formatterV1.date(from: string) {
return date
}
let formatterV2 = ISO8601DateFormatter()
formatterV2.formatOptions.insert(.withFractionalSeconds)
if let date = formatterV2.date(from: string) {
return date
}
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date: \(string)")
}
}

View File

@ -1,9 +1,10 @@
//
// File.swift
// Mastodon+Entity.swift
//
//
// Created by xiaojian sun on 2021/1/25.
//
import Foundation
extension Mastodon.Entity { }

View File

@ -1,5 +1,5 @@
//
// File.swift
// Mastodon.swift
//
//
// Created by xiaojian sun on 2021/1/25.

View File

@ -6,7 +6,6 @@ final class MastodonSDKTests: XCTestCase {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(MastodonSDK().text, "Hello, World!")
}
static var allTests = [