mount public data from the atmosphere to a virtual filesystem (macos only) pdfs.at
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(atproto): add XRPCClient POST with JSON + raw body

+52
+27
Packages/ATProto/Sources/ATProto/XRPC/XRPCClient.swift
··· 28 28 return try await sendRaw(request) 29 29 } 30 30 31 + public func post<Input: Encodable, Output: Decodable>( 32 + nsid: String, 33 + input: Input, 34 + params: [String: String]? = nil 35 + ) async throws -> Output { 36 + let url = try buildURL(nsid: nsid, params: params) 37 + var request = URLRequest(url: url) 38 + request.httpMethod = "POST" 39 + request.addValue("application/json", forHTTPHeaderField: "Content-Type") 40 + request.httpBody = try JSONEncoder().encode(input) 41 + return try await send(request) 42 + } 43 + 44 + public func postRaw( 45 + nsid: String, 46 + body: Data, 47 + contentType: String, 48 + params: [String: String]? = nil 49 + ) async throws -> (Data, URLResponse) { 50 + let url = try buildURL(nsid: nsid, params: params) 51 + var request = URLRequest(url: url) 52 + request.httpMethod = "POST" 53 + request.addValue(contentType, forHTTPHeaderField: "Content-Type") 54 + request.httpBody = body 55 + return try await sendRaw(request) 56 + } 57 + 31 58 func buildURL(nsid: String, params: [String: String]?) throws -> URL { 32 59 guard var comps = URLComponents(url: service, resolvingAgainstBaseURL: false) else { 33 60 throw ATProtoError.invalidURL(service.absoluteString)
+25
Packages/ATProto/Tests/ATProtoTests/XRPCClientTests.swift
··· 92 92 let result: Echo = try await client.get(nsid: "com.example.echo", params: nil) 93 93 #expect(result == Echo(value: "ok")) 94 94 } 95 + 96 + @Test("POST sends JSON body and decodes response") 97 + func postSuccess() async throws { 98 + struct In: Encodable { let name: String } 99 + struct Out: Decodable, Equatable { let ok: Bool } 100 + 101 + let session = URLProtocolStub.install { request in 102 + #expect(request.httpMethod == "POST") 103 + #expect(request.value(forHTTPHeaderField: "Content-Type") == "application/json") 104 + #expect(request.url?.path == "/xrpc/com.example.create") 105 + return .json(#"{"ok":true}"#) 106 + } 107 + defer { URLProtocolStub.reset() } 108 + 109 + let client = XRPCClient( 110 + service: URL(string: "https://bsky.social")!, 111 + session: session, 112 + auth: AnonymousAuthTokenProvider() 113 + ) 114 + let result: Out = try await client.post( 115 + nsid: "com.example.create", 116 + input: In(name: "hi") 117 + ) 118 + #expect(result == Out(ok: true)) 119 + } 95 120 }