Skip to content

Fix crash and httpBody not being set #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PostgrestBuilder {
}

let session = URLSession.shared
let dataTask = session.dataTask(with: request, completionHandler: { [unowned self] (data, response, error) -> Void in
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if let error = error {
completion(.failure(error))
return
Expand All @@ -47,8 +47,8 @@ public class PostgrestBuilder {
}

do {
try validate(data: data, response: response)
let response = try parse(data: data, response: response)
try Self.validate(data: data, response: response)
let response = try Self.parse(data: data, response: response, request: request)
completion(.success(response))
} catch {
completion(.failure(error))
Expand All @@ -58,7 +58,7 @@ public class PostgrestBuilder {
dataTask.resume()
}

private func validate(data: Data, response: HTTPURLResponse) throws {
private static func validate(data: Data, response: HTTPURLResponse) throws {
if 200 ..< 300 ~= response.statusCode {
return
}
Expand All @@ -70,11 +70,11 @@ public class PostgrestBuilder {
throw PostgrestError(from: json) ?? PostgrestError(message: "failed to get error")
}

private func parse(data: Data, response: HTTPURLResponse) throws -> PostgrestResponse {
private static func parse(data: Data, response: HTTPURLResponse, request: URLRequest) throws -> PostgrestResponse {
var body: Any = data
var count: Int?

if method == "HEAD" {
if request.httpMethod == "HEAD" {
if let accept = response.allHeaderFields["Accept"] as? String, accept == "text/csv" {
body = data
} else {
Expand Down Expand Up @@ -138,6 +138,9 @@ public class PostgrestBuilder {
var request = URLRequest(url: url)
request.httpMethod = method
request.allHTTPHeaderFields = headers
if let body = body {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
return request
}

Expand Down
13 changes: 5 additions & 8 deletions Sources/PostgREST/PostgrestError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ public struct PostgrestError: Error {
public var message: String

init?(from dictionary: [String: Any]) {
guard let details = dictionary["details"] as? String,
let hint = dictionary["hint"] as? String,
let code = dictionary["code"] as? String,
let message = dictionary["message"] as? String
else {
guard let message = dictionary["message"] as? String else {
return nil
}
self.details = details
self.hint = hint
self.code = code

self.details = dictionary["details"] as? String
self.hint = dictionary["hint"] as? String
self.code = dictionary["code"] as? String
self.message = message
}

Expand Down
5 changes: 5 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ final class BuildURLRequestTests: XCTestCase {
.select()
.like(column: "email", value: "%@supabase.co")
.buildURLRequest(head: false, count: nil)
},
TestCase(name: "insert new user") { client in
try client.form("users")
.insert(values: ["email": "[email protected]"])
.buildURLRequest(head: false, count: nil)
}
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl \
--request POST \
--header "Prefer: return=representation" \
--data "{\"email\":\"[email protected]\"}" \
"https://example.supabase.co/users"