Post request for refined searches
This commit is contained in:
@@ -16,8 +16,8 @@ public final class USDA_FDC_Client {
|
||||
public func getFood(_ foodCriteria: FoodCriteria) async throws -> AbridgedFoodItem {
|
||||
var endpoint = baseURL.appendingPathComponent("/food/\(foodCriteria.fdcId)")
|
||||
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
||||
// if criteria != nil {
|
||||
// for (key, value) in Mirror(reflecting: criteria!).children {
|
||||
// if foodCriteria as Any? != nil {
|
||||
// for (key, value) in Mirror(reflecting: foodCriteria!).children {
|
||||
// if value as Any? != nil {
|
||||
// if let l = value as? [String] {
|
||||
// endpoint.append(queryItems: [.init(name: key!, value: String(describing: l.joined(separator: ",")))])
|
||||
@@ -26,7 +26,6 @@ public final class USDA_FDC_Client {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
print(endpoint)
|
||||
var request = URLRequest(url: endpoint)
|
||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
@@ -54,7 +53,6 @@ public final class USDA_FDC_Client {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
print(endpoint)
|
||||
var request = URLRequest(url: endpoint)
|
||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
@@ -68,10 +66,16 @@ public final class USDA_FDC_Client {
|
||||
}
|
||||
}
|
||||
|
||||
public func getFoodsSearch(_ search: FoodSearchCriteria) async throws -> SearchResult {
|
||||
public enum HTTP_Method: String {
|
||||
case GET, POST
|
||||
}
|
||||
|
||||
public func searchFoods(_ search: FoodSearchCriteria, httpMethod: HTTP_Method = .GET) async throws -> SearchResult {
|
||||
var endpoint = baseURL.appendingPathComponent("/foods/search")
|
||||
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
||||
if httpMethod == .GET {
|
||||
endpoint.append(queryItems: [.init(name: "query", value: search.query)])
|
||||
}
|
||||
// if criteria != nil {
|
||||
// for (key, value) in Mirror(reflecting: criteria!).children {
|
||||
// if value as Any? != nil {
|
||||
@@ -82,9 +86,13 @@ public final class USDA_FDC_Client {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
print(endpoint)
|
||||
var request = URLRequest(url: endpoint)
|
||||
request.httpMethod = httpMethod.rawValue
|
||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||
if httpMethod == .POST {
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = try JSONEncoder().encode(search)
|
||||
}
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||
throw FDCError.invalidResponse
|
||||
@@ -109,7 +117,6 @@ public final class USDA_FDC_Client {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
print(endpoint)
|
||||
var request = URLRequest(url: endpoint)
|
||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
public struct FoodSearchCriteria: Codable {
|
||||
public var query: String
|
||||
public var dataType: DataType?
|
||||
public var dataType: [DataType]?
|
||||
public var pageSize: Int?
|
||||
public var pageNumber: Int?
|
||||
public var sortBy: SortBy?
|
||||
|
||||
@@ -49,11 +49,23 @@ final class USDATests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testFoodSearch() async throws {
|
||||
func testGetFoodSearch() async throws {
|
||||
let fdc = USDA_FDC_Client()
|
||||
fdc.apiKey = env["API_KEY"] ?? ""
|
||||
do {
|
||||
let _ = try await fdc.getFoodsSearch(.init(query: "cheddar cheese"))
|
||||
let _ = try await fdc.searchFoods(.init(query: "cheddar cheese"))
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testPostFoodSearch() async throws {
|
||||
let fdc = USDA_FDC_Client()
|
||||
fdc.apiKey = env["API_KEY"] ?? ""
|
||||
do {
|
||||
let res = try await fdc.searchFoods(.init(query: "037600106214", dataType: [.Branded]), httpMethod: .POST)
|
||||
print(res)
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user