diff --git a/Sources/USDA_FDC/USDA_FDC.swift b/Sources/USDA_FDC/USDA_FDC.swift index 146e13b..9983247 100644 --- a/Sources/USDA_FDC/USDA_FDC.swift +++ b/Sources/USDA_FDC/USDA_FDC.swift @@ -16,17 +16,16 @@ 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 value as Any? != nil { - // 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: value))]) - // } - // } + // 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: ",")))]) + // } + // endpoint.append(queryItems: [.init(name: key!, value: String(describing: value))]) + // } + // } // } - 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)]) - endpoint.append(queryItems: [.init(name: "query", value: search.query)]) + 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) diff --git a/Sources/USDA_FDC/types/FoodSearchCriteria.swift b/Sources/USDA_FDC/types/FoodSearchCriteria.swift index 35dfa89..6e78a74 100644 --- a/Sources/USDA_FDC/types/FoodSearchCriteria.swift +++ b/Sources/USDA_FDC/types/FoodSearchCriteria.swift @@ -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? diff --git a/Tests/USDATests/USDATests.swift b/Tests/USDATests/USDATests.swift index 003cb1e..857cc59 100644 --- a/Tests/USDATests/USDATests.swift +++ b/Tests/USDATests/USDATests.swift @@ -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 }