aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFinn Behrens <me@kloenk.dev>2021-09-29 10:44:48 +0200
committerFinn Behrens <me@kloenk.dev>2021-09-29 10:44:48 +0200
commite03e5d13c189dbf0ff8bbadcce9a8af0914a5b44 (patch)
treea0860c4d1034c84d4031e3886edb48335b07d7b1
parentbcef3014398f4a941ec87c15344905a10b9a6130 (diff)
downloadMatrixCore-e03e5d13c189dbf0ff8bbadcce9a8af0914a5b44.tar.gz
MatrixCore-e03e5d13c189dbf0ff8bbadcce9a8af0914a5b44.tar.xz
MatrixCore-e03e5d13c189dbf0ff8bbadcce9a8af0914a5b44.zip
add LoginFlow to protocol
-rw-r--r--Sources/MatrixClient/API/HomeServer.swift4
-rw-r--r--Sources/MatrixClient/API/Login.swift (renamed from Sources/MatrixClient/Login.swift)22
-rw-r--r--Sources/MatrixClient/API/Request.swift15
-rw-r--r--Sources/MatrixClient/MatrixClient.swift57
4 files changed, 37 insertions, 61 deletions
diff --git a/Sources/MatrixClient/API/HomeServer.swift b/Sources/MatrixClient/API/HomeServer.swift
index db8a853..379a047 100644
--- a/Sources/MatrixClient/API/HomeServer.swift
+++ b/Sources/MatrixClient/API/HomeServer.swift
@@ -38,7 +38,7 @@ public struct MatrixServerInfoRequest: MatrixRequest {
return "/_matrix/client/versions"
}
- public static var httpMethod = "GET"
+ public static var httpMethod = HttpMethode.GET
public static var requiresAuth = false
}
@@ -66,7 +66,7 @@ public struct MatrixWellKnownRequest: MatrixRequest {
return "/.well-known/matrix/client"
}
- public static var httpMethod = "GET"
+ public static var httpMethod = HttpMethode.GET
public static var requiresAuth = false
}
diff --git a/Sources/MatrixClient/Login.swift b/Sources/MatrixClient/API/Login.swift
index fc70cdb..624dcdb 100644
--- a/Sources/MatrixClient/Login.swift
+++ b/Sources/MatrixClient/API/Login.swift
@@ -7,6 +7,28 @@
import Foundation
+public struct MatrixLoginFlowRequest: MatrixRequest {
+ public typealias Response = Self.ResponseStruct
+
+ public typealias URLParameters = ()
+
+ public func path(with parameters: ()) -> String {
+ return "/_matrix/client/r0/login"
+ }
+
+ public static var httpMethod = HttpMethode.GET
+
+ public static var requiresAuth = false
+
+ public struct ResponseStruct: MatrixResponse {
+ var flows: [FlowType]
+
+ struct FlowType: Codable {
+ var type: MatrixLoginFlow
+ }
+ }
+}
+
public struct MatrixLoginFlow: Codable {
public var value: String
diff --git a/Sources/MatrixClient/API/Request.swift b/Sources/MatrixClient/API/Request.swift
index 1f2cc0d..3d60af1 100644
--- a/Sources/MatrixClient/API/Request.swift
+++ b/Sources/MatrixClient/API/Request.swift
@@ -7,13 +7,22 @@
import Foundation
+public enum HttpMethode: String, CaseIterable {
+ case GET = "GET"
+ case POST = "POST"
+ case PUT = "PUT"
+ case PATCH = "PATCH"
+
+ static var containsBoddy: [Self] = [ .POST, .PUT, .PATCH ]
+}
+
public protocol MatrixRequest: Codable {
associatedtype Response: MatrixResponse
associatedtype URLParameters
func path(with parameters: URLParameters) -> String
- static var httpMethod: String { get }
+ static var httpMethod: HttpMethode { get }
static var requiresAuth: Bool { get }
// TODO: rate limited property?
}
@@ -32,8 +41,8 @@ public extension MatrixRequest {
urlRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
- urlRequest.httpMethod = Self.httpMethod
- if ["POST", "PUT", "PATCH"].contains(Self.httpMethod) {
+ urlRequest.httpMethod = Self.httpMethod.rawValue
+ if HttpMethode.containsBoddy.contains(Self.httpMethod) {
urlRequest.httpBody = try? JSONEncoder().encode(self)
}
diff --git a/Sources/MatrixClient/MatrixClient.swift b/Sources/MatrixClient/MatrixClient.swift
index 32bcd8b..f11d328 100644
--- a/Sources/MatrixClient/MatrixClient.swift
+++ b/Sources/MatrixClient/MatrixClient.swift
@@ -56,65 +56,10 @@ public struct MatrixClient {
/// Requires auth: No.
/// ```
public func getLoginFlows() async throws -> [MatrixLoginFlow] {
- struct FlowResponse: Codable {
- var flows: [FlowType]
-
- struct FlowType: Codable {
- var type: MatrixLoginFlow
- }
- }
-
- let flows = try await request("/_matrix/client/r0/login", withAuthorization: false, forType: FlowResponse.self)
+ let flows = try await MatrixLoginFlowRequest().repsonse(on: homeserver, with: (), withUrlSession: urlSession)
return flows.flows.map({$0.type})
}
-
- // Mark: - Helpers
- func urlComponents(_ path: String) throws -> URL {
- var components = self.homeserver.url
-
- components.path = path
-
- guard let url = components.url else {
- throw MatrixError.NotFound
- }
- return url
- }
-
- func urlRequest(_ path: String, withAuthorization: Bool = true) throws -> URLRequest {
- var requst = URLRequest(url: try urlComponents(path))
-
- if withAuthorization {
- guard let accessToken = accessToken else {
- throw MatrixError.Forbidden
- }
- requst.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
- }
-
- return requst
- }
-
- func request(_ path: String, withAuthorization: Bool = true) async throws -> Data {
- let request = try urlRequest(path, withAuthorization: withAuthorization)
-
- let (data, urlResponse) = try await urlSession.data(for: request)
-
- guard let response = urlResponse as? HTTPURLResponse else {
- throw MatrixError.Unknown
- }
- guard response.statusCode == 200 else {
- throw try MatrixServerError(json: data, code: response.statusCode)
- }
-
- return data
- }
-
- func request<T: Decodable>(_ path: String, withAuthorization: Bool = true, forType type: T.Type) async throws -> T {
- let data = try await request(path, withAuthorization: withAuthorization)
-
- let decoder = JSONDecoder()
- return try decoder.decode(type, from: data)
- }
}