@@ -18,14 +18,26 @@ 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+ 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+
2941 static func post( message: String ) async throws {
3042 @Dependency ( \. environment) var environment
3143 @Dependency ( \. httpClient) var httpClient
@@ -43,15 +55,7 @@ enum Mastodon {
4355 var status : String
4456 }
4557
46- var components = URLComponents ( )
47- components. scheme = " https "
48- components. host = Mastodon . instance
49- components. path = " /api/v1/statuses "
50- components. queryItems = [ URLQueryItem ( name: " status " , value: message) ]
51- guard let url = components. string else {
52- throw Social . Error. invalidURL
53- }
54- let res = try await httpClient. post ( url: url, headers: headers, body: nil )
58+ let res = try await httpClient. post ( url: apiURL ( with: message) , headers: headers, body: nil )
5559
5660 guard res. status == . ok else {
5761 throw Social . Error. requestFailed ( res. status, res. body? . asString ( ) ?? " " )
0 commit comments