-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathEndpoints.swift
93 lines (79 loc) · 2.84 KB
/
Endpoints.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
import Foundation
/// An endpoint for exposed people
struct ExposeeEndpoint {
/// The base URL to derive the url from
let baseURL: URL
/// A version of the API
let version: String
/// Initialize the endpoint
/// - Parameters:
/// - baseURL: The base URL of the endpoint
/// - version: The version of the API
init(baseURL: URL, version: String = "v2") {
self.baseURL = baseURL
self.version = version
}
/// A versionned base URL
private var baseURLVersionned: URL {
baseURL.appendingPathComponent(version)
}
/// Get the URL for the exposed people endpoint for a given lastKeyBundleTag
/// - Parameters:
/// - lastKeyBundleTag: last published key tag if one is stored
func getExposee(lastKeyBundleTag: String?, federationGateway: FederationGateway) -> URL {
let url = baseURLVersionned.appendingPathComponent("gaen")
.appendingPathComponent("exposed")
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
var queryItems: [URLQueryItem] = []
if let lastKeyBundleTag = lastKeyBundleTag {
queryItems.append(URLQueryItem(name: "lastKeyBundleTag", value: lastKeyBundleTag))
}
switch federationGateway {
case .yes:
queryItems.append(URLQueryItem(name: "withFederationGateway", value: "true"))
case .no:
queryItems.append(URLQueryItem(name: "withFederationGateway", value: "false"))
case .unspecified:
break
}
if !queryItems.isEmpty {
urlComponents?.queryItems = queryItems
}
guard let finalUrl = urlComponents?.url else {
fatalError("can't create URLComponents url")
}
return finalUrl
}
}
/// An endpoint for adding and removing exposed people
struct ManagingExposeeEndpoint {
/// The base URL to derive the url from
let baseURL: URL
/// A version of the API
let version: String
/// Initialize the endpoint
/// - Parameters:
/// - baseURL: The base URL of the endpoint
/// - version: The version of the API
init(baseURL: URL, version: String = "v2") {
self.baseURL = baseURL
self.version = version
}
/// A versionned base URL
private var baseURLVersionned: URL {
baseURL.appendingPathComponent(version)
}
/// Get the add exposed endpoint URL
func addExposedGaen() -> URL {
baseURLVersionned.appendingPathComponent("gaen").appendingPathComponent("exposed")
}
}