@@ -18,34 +18,45 @@ import Vapor
1818
1919enum Mastodon {
2020
21- private static let instance = " mas.to "
22- private static let apiURL = " https:// \( instance ) /api/v1/statuses "
21+ private static let apiHost = " mas.to "
22+ private static let apiPath = " /api/v1/statuses "
2323 static let postMaxLength = 490 // 500, leaving some buffer for unicode accounting oddities
2424
2525 struct Credentials {
2626 var accessToken : String
2727 }
2828
29- // NB: _testEncodedURL is a callback that exists purely to be able to regression test the encoded value
30- static func post( client: Client , message: String , _testEncodedURL: ( String ) -> Void = { _ in } ) async throws {
29+ static func apiURL( with message: String ) throws -> String {
30+ var components = URLComponents ( )
31+ components. scheme = " https "
32+ components. host = apiHost
33+ components. path = apiPath
34+ components. queryItems = [ URLQueryItem ( name: " status " , value: message) ]
35+ guard let url = components. string else {
36+ throw Social . Error. invalidURL
37+ }
38+ return url
39+ }
40+
41+ static func post( message: String ) async throws {
3142 @Dependency ( \. environment) var environment
43+ @Dependency ( \. httpClient) var httpClient
44+ @Dependency ( \. uuid) var uuid
3245 guard let credentials = environment. mastodonCredentials ( ) else {
3346 throw Social . Error. missingCredentials
3447 }
3548
3649 let headers = HTTPHeaders ( [
3750 ( " Authorization " , " Bearer \( credentials. accessToken) " ) ,
38- ( " Idempotency-Key " , UUID ( ) . uuidString) ,
51+ ( " Idempotency-Key " , uuid ( ) . uuidString) ,
3952 ] )
4053
4154 struct Query : Encodable {
4255 var status : String
4356 }
4457
45- let res = try await client. post ( URI ( string: apiURL) , headers: headers) { req in
46- try req. query. encode ( Query ( status: message) )
47- _testEncodedURL ( req. url. string)
48- }
58+ let res = try await httpClient. post ( url: apiURL ( with: message) , headers: headers, body: nil )
59+
4960 guard res. status == . ok else {
5061 throw Social . Error. requestFailed ( res. status, res. body? . asString ( ) ?? " " )
5162 }
0 commit comments