-
Notifications
You must be signed in to change notification settings - Fork 0
/
USDA.swift
127 lines (94 loc) · 4.13 KB
/
USDA.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// USDA.swift
// RestARant
//
// Created by Maxwell Hubbard on 10/13/18.
// Copyright © 2018 Maxwell Hubbard. All rights reserved.
//
import Foundation
class USDA {
static let API_KEY: String! = "CwgyRewjqXjZKpNSBkEXsVKoCOvnvrg42948f6bW"
static var SEARCH_URL: String!
static func search(s: String, ds: String, closure: @escaping (_ item: [String: Any]) -> Void) {
SEARCH_URL = "https://api.nal.usda.gov/ndb/search/?format=json&ds=" + ds.replacingOccurrences(of: " ", with: "%20") + "&q=" + s.replacingOccurrences(of: " ", with: "%20") + "&sort=r&max=50&offset=0&api_key=" + API_KEY
let session = URLSession.shared
let task = session.dataTask(with: URLRequest(url: URL(string: SEARCH_URL)!)) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let json = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
guard let list = json["list"] as? [String: Any] else {
print("Could not get list from JSON")
return
}
guard let items = list["item"] as? [[String: Any]] else {
print("Could not get items from JSON")
return
}
closure(items[0])
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
static func getNutrients(id: String, closure: @escaping (_ nutrients: [[String : Any]]) -> Void) {
let INFO_ID = "https://api.nal.usda.gov/ndb/reports/?ndbno=" + id + "&type=f&format=json&api_key=" + API_KEY
let session = URLSession.shared
let task = session.dataTask(with: URLRequest(url: URL(string: INFO_ID)!)) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard let json = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
guard let list = json["report"] as? [String: Any] else {
print("Could not get report from JSON")
return
}
guard let items = list["food"] as? [String: Any] else {
print("Could not get food from JSON")
return
}
guard let nutrients = items["nutrients"] as? [[String: Any]] else {
print("Could not get nutrients from JSON")
return
}
closure(nutrients)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
}