Post request for refined searches
This commit is contained in:
@@ -16,17 +16,16 @@ public final class USDA_FDC_Client {
|
|||||||
public func getFood(_ foodCriteria: FoodCriteria) async throws -> AbridgedFoodItem {
|
public func getFood(_ foodCriteria: FoodCriteria) async throws -> AbridgedFoodItem {
|
||||||
var endpoint = baseURL.appendingPathComponent("/food/\(foodCriteria.fdcId)")
|
var endpoint = baseURL.appendingPathComponent("/food/\(foodCriteria.fdcId)")
|
||||||
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
||||||
// if criteria != nil {
|
// if foodCriteria as Any? != nil {
|
||||||
// for (key, value) in Mirror(reflecting: criteria!).children {
|
// for (key, value) in Mirror(reflecting: foodCriteria!).children {
|
||||||
// if value as Any? != nil {
|
// if value as Any? != nil {
|
||||||
// if let l = value as? [String] {
|
// if let l = value as? [String] {
|
||||||
// endpoint.append(queryItems: [.init(name: key!, value: String(describing: l.joined(separator: ",")))])
|
// endpoint.append(queryItems: [.init(name: key!, value: String(describing: l.joined(separator: ",")))])
|
||||||
// }
|
// }
|
||||||
// endpoint.append(queryItems: [.init(name: key!, value: String(describing: value))])
|
// endpoint.append(queryItems: [.init(name: key!, value: String(describing: value))])
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
print(endpoint)
|
|
||||||
var request = URLRequest(url: endpoint)
|
var request = URLRequest(url: endpoint)
|
||||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
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)
|
var request = URLRequest(url: endpoint)
|
||||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
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")
|
var endpoint = baseURL.appendingPathComponent("/foods/search")
|
||||||
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
endpoint.append(queryItems: [.init(name: "api_key", value: apiKey)])
|
||||||
endpoint.append(queryItems: [.init(name: "query", value: search.query)])
|
if httpMethod == .GET {
|
||||||
|
endpoint.append(queryItems: [.init(name: "query", value: search.query)])
|
||||||
|
}
|
||||||
// if criteria != nil {
|
// if criteria != nil {
|
||||||
// for (key, value) in Mirror(reflecting: criteria!).children {
|
// for (key, value) in Mirror(reflecting: criteria!).children {
|
||||||
// if value as Any? != nil {
|
// if value as Any? != nil {
|
||||||
@@ -82,9 +86,13 @@ public final class USDA_FDC_Client {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
print(endpoint)
|
|
||||||
var request = URLRequest(url: endpoint)
|
var request = URLRequest(url: endpoint)
|
||||||
|
request.httpMethod = httpMethod.rawValue
|
||||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
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)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||||
throw FDCError.invalidResponse
|
throw FDCError.invalidResponse
|
||||||
@@ -109,7 +117,6 @@ public final class USDA_FDC_Client {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
print(endpoint)
|
|
||||||
var request = URLRequest(url: endpoint)
|
var request = URLRequest(url: endpoint)
|
||||||
request.setValue("application/json", forHTTPHeaderField: "accept")
|
request.setValue("application/json", forHTTPHeaderField: "accept")
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
public struct FoodSearchCriteria: Codable {
|
public struct FoodSearchCriteria: Codable {
|
||||||
public var query: String
|
public var query: String
|
||||||
public var dataType: DataType?
|
public var dataType: [DataType]?
|
||||||
public var pageSize: Int?
|
public var pageSize: Int?
|
||||||
public var pageNumber: Int?
|
public var pageNumber: Int?
|
||||||
public var sortBy: SortBy?
|
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()
|
let fdc = USDA_FDC_Client()
|
||||||
fdc.apiKey = env["API_KEY"] ?? ""
|
fdc.apiKey = env["API_KEY"] ?? ""
|
||||||
do {
|
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 {
|
} catch {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user