|
22 | 22 | // limitations under the License.
|
23 | 23 |
|
24 | 24 | import Foundation
|
25 |
| -import SwiftyJSON |
26 | 25 | import Alamofire
|
27 | 26 | import Hydra
|
28 | 27 | import GRDB
|
@@ -59,7 +58,7 @@ public class MastodonUserToken: Equatable, @unchecked Sendable {
|
59 | 58 | }
|
60 | 59 |
|
61 | 60 | public func getIntVersion() async throws -> MastodonVersionInt {
|
62 |
| - return MastodonVersionInt(try await self.app.instance.getInfo()["version"].stringValue) |
| 61 | + return MastodonVersionInt(try await self.app.instance.getInfo().version) |
63 | 62 | }
|
64 | 63 |
|
65 | 64 | static public func initFromId(id: String) -> MastodonUserToken? {
|
@@ -201,25 +200,20 @@ public class MastodonUserToken: Equatable, @unchecked Sendable {
|
201 | 200 | }
|
202 | 201 | }
|
203 | 202 |
|
204 |
| - static var verifyCredentialsCache: [String: JSON] = [:] |
| 203 | + static var verifyCredentialsCache: [String: MastodonEndpoint.VerifyCredentials.Response] = [:] |
205 | 204 |
|
206 |
| - public func getUserInfo(cache: Bool = false) async throws -> JSON { |
| 205 | + public func getUserInfo(cache: Bool = false) async throws -> MastodonEndpoint.VerifyCredentials.Response { |
207 | 206 | if cache, let cacheObj = MastodonUserToken.verifyCredentialsCache[self.acct] {
|
208 | 207 | return cacheObj
|
209 | 208 | }
|
210 |
| - let response = try await getJSON("accounts/verify_credentials") |
211 |
| - self.name = response["display_name"].string |
212 |
| - if self.name == nil || self.name?.isEmpty == true { |
213 |
| - self.name = response["name"].string |
214 |
| - } |
215 |
| - self.screenName = response["username"].string |
216 |
| - self.avatarUrl = response["avatar"].string |
| 209 | + let response = try await MastodonEndpoint.VerifyCredentials().request(with: self) |
| 210 | + self.name = response.displayName.isEmpty ? response.screenName : response.displayName |
| 211 | + self.screenName = response.screenName |
| 212 | + self.avatarUrl = response.avatar |
217 | 213 | if let avatarUrl = self.avatarUrl, !avatarUrl.isEmpty, avatarUrl.first == "/" { // ホスト名がない!!!
|
218 | 214 | self.avatarUrl = "https://"+self.app.instance.hostName+self.avatarUrl!
|
219 | 215 | }
|
220 |
| - if response["error"].isEmpty { |
221 |
| - MastodonUserToken.verifyCredentialsCache[self.acct] = response |
222 |
| - } |
| 216 | + MastodonUserToken.verifyCredentialsCache[self.acct] = response |
223 | 217 | return response
|
224 | 218 | }
|
225 | 219 |
|
@@ -257,35 +251,6 @@ public class MastodonUserToken: Equatable, @unchecked Sendable {
|
257 | 251 | )
|
258 | 252 | }
|
259 | 253 |
|
260 |
| - @available(*, deprecated) |
261 |
| - func getJSON(_ endpoint: String, params: [URLQueryItem] = []) async throws -> JSON { |
262 |
| - print("GET", endpoint) |
263 |
| - |
264 |
| - var urlBuilder = URLComponents(url: URL(string: endpoint, relativeTo: URL(string: "https://\(self.app.instance.hostName)/api/v1/")!)!, resolvingAgainstBaseURL: true)! |
265 |
| - urlBuilder.queryItems = params |
266 |
| - if urlBuilder.queryItems?.count == 0 { |
267 |
| - urlBuilder.queryItems = nil |
268 |
| - } |
269 |
| - let headers = getHeader() |
270 |
| - var request = URLRequest(url: try urlBuilder.asURL()) |
271 |
| - print(request.url) |
272 |
| - request.httpMethod = "GET" |
273 |
| - for (name, value) in headers { |
274 |
| - request.setValue(value, forHTTPHeaderField: name) |
275 |
| - } |
276 |
| - print(request.httpMethod!, request.url!) |
277 |
| - let (data, response) = try await URLSession.shared.data(for: request) |
278 |
| - if let response = response as? HTTPURLResponse, response.statusCode >= 400 { |
279 |
| - do { |
280 |
| - let error = try JSONDecoder.forMastodonAPI.decode(MastodonErrorResponse.self, from: data) |
281 |
| - throw APIError.errorReturned(errorMessage: error.error, errorHttpCode: response.statusCode) |
282 |
| - } catch { |
283 |
| - throw APIError.unknownResponse(errorHttpCode: response.statusCode, errorString: .init(data: data, encoding: .utf8)) |
284 |
| - } |
285 |
| - } |
286 |
| - return try JSON(data: data) |
287 |
| - } |
288 |
| - |
289 | 254 | public func upload(file: Data, mimetype: String, filename: String = "imast_upload_file") async throws -> MastodonAttachment {
|
290 | 255 | let request = try await withCheckedThrowingContinuation { continuation in
|
291 | 256 | Alamofire.upload(
|
|
0 commit comments