Skip to content

Commit 6583648

Browse files
author
Guilherme Souza
committed
Move PostgrestResponse validation and creation
1 parent c5ddf25 commit 6583648

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

Sources/PostgREST/PostgrestBuilder.swift

+4-24
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class PostgrestBuilder {
3131
/// - count: A `CountOption` determining how many items to return. Defaults to `nil`
3232
/// - completion: Escaping completion handler with either a `PostgrestResponse` or an `Error`. Called after API call is completed and validated.
3333
public func execute(
34-
head: Bool = false, count: CountOption? = nil,
34+
head: Bool = false,
35+
count: CountOption? = nil,
3536
completion: @escaping (Result<PostgrestResponse, Error>) -> Void
3637
) {
3738
let request: URLRequest
@@ -63,7 +64,7 @@ public class PostgrestBuilder {
6364

6465
do {
6566
try Self.validate(data: data, response: response)
66-
let response = try Self.parse(data: data, response: response, request: request)
67+
let response = PostgrestResponse(data: data, response: response)
6768
completion(.success(response))
6869
} catch {
6970
completion(.failure(error))
@@ -83,28 +84,7 @@ public class PostgrestBuilder {
8384
return
8485
}
8586

86-
throw try JSONDecoder().decode(PostgrestError.self, from: data)
87-
}
88-
89-
/// Parses incoming data and server response into a `PostgrestResponse`
90-
/// - Parameters:
91-
/// - data: Data received from the server
92-
/// - response: Response received from the server
93-
/// - Throws: Throws an `Error` if invalid JSON.
94-
/// - Returns: Returns a `PostgrestResponse`
95-
private static func parse(data: Data, response: HTTPURLResponse, request: URLRequest) throws
96-
-> PostgrestResponse
97-
{
98-
var count: Int?
99-
100-
if let contentRange = response.allHeaderFields["content-range"] as? String,
101-
let lastElement = contentRange.split(separator: "/").last
102-
{
103-
count = lastElement == "*" ? nil : Int(lastElement)
104-
}
105-
106-
let postgrestResponse = PostgrestResponse(data: data, status: response.statusCode, count: count)
107-
return postgrestResponse
87+
throw try JSONDecoder.postgrest.decode(PostgrestError.self, from: data)
10888
}
10989

11090
/// Builds the URL request for PostgREST

Sources/PostgREST/PostgrestResponse.swift

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ public struct PostgrestResponse: Hashable {
1010
self.status = status
1111
self.count = count
1212
}
13+
14+
public init(data: Data, response: HTTPURLResponse) {
15+
var count: Int?
16+
17+
if let contentRange = response.allHeaderFields["content-range"] as? String,
18+
let lastElement = contentRange.split(separator: "/").last
19+
{
20+
count = lastElement == "*" ? nil : Int(lastElement)
21+
}
22+
23+
self.init(data: data, status: response.statusCode, count: count)
24+
}
1325
}
1426

1527
extension PostgrestResponse {

0 commit comments

Comments
 (0)