Instance Method
get(fromDB:uri:queryItems:eventLoopGroup:)
Fetches raw data from a specified database and URI on the CouchDB server.
func get(fromDB dbName: String, uri: String, queryItems: [URLQueryItem]? = nil, eventLoopGroup: (any EventLoopGroup)? = nil) async throws -> HTTPClientResponse
Parameters
- dbName
The name of the database from which to fetch data.
- uri
The URI path of the specific resource or endpoint within the database (e.g., document ID or view path).
- queryItems
An optional array of URLQueryItem to specify query parameters for the request.
- eventLoopGroup
An optional EventLoopGroup for executing network operations. If not provided, the function defaults to using a shared instance of HTTPClient.
Return Value
An HTTPClientResponse whose body has already been buffered in memory.
Discussion
This method sends a GET request to a database resource and returns the raw HTTPClientResponse. Before returning, it buffers the response body in memory so callers can inspect or decode it without reissuing the request. It supports using a custom EventLoopGroup and optional query parameters.
Throws
A CouchDBClientError if authentication fails, the resource is not found, or the response body is missing.
Example Usage:
Define Your Document Data Model
struct ExpectedDoc: CouchDBRepresentable {
var name: String
var _id: String = NSUUID().uuidString
var _rev: String?
func updateRevision(_ newRevision: String) -> Self {
return ExpectedDoc(name: name, _id: _id, _rev: newRevision)
}
}
Fetch a Raw Response:
let response = try await couchDBClient.get(
fromDB: "myDatabase",
uri: "documentID"
)
print(response.status)
Fetch Data for Manual Decoding:
let response = try await couchDBClient.get(
fromDB: "myDatabase",
uri: "_design/all/_view/by_url",
queryItems: [
URLQueryItem(name: "key", value: "\"\(url)\"")
]
)
print(response.status)
Note
Ensure that the CouchDB server is running and accessible. Handle thrown errors appropriately, especially for authentication issues.