Skip to content

Commit 404533c

Browse files
committed
initial commit
1 parent 79791d0 commit 404533c

4 files changed

Lines changed: 39 additions & 83 deletions

File tree

Sources/AgroAPI/AgroClient.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class AgroClient {
4343
public let sessionManager: URLSession
4444

4545
public let mediaType = "application/json; charset=utf-8"
46+
4647
public let agroPolyURL = "https://api.agromonitoring.com/agro/1.0/polygons"
4748
public let agroSatURL = "https://api.agromonitoring.com/agro/1.0/image"
4849
public let agroWeatherURL = "https://api.agromonitoring.com/agro/1.0/weather"
@@ -158,7 +159,23 @@ public class AgroClient {
158159
guard let url = urlHistoryBuilder(options: options) else {
159160
return Just<T?>(nil).setFailureType(to: AgroAPIError.self).eraseToAnyPublisher()
160161
}
161-
print("\n---> url: \(url)")
162+
var request = URLRequest(url: url)
163+
request.httpMethod = "GET"
164+
request.addValue(mediaType, forHTTPHeaderField: "Accept")
165+
request.addValue(mediaType, forHTTPHeaderField: "Content-Type")
166+
167+
return self.doDataTaskPublish(request: request)
168+
}
169+
170+
/// fetch data from the server. A GET request with the chosen options is sent to the server.
171+
/// The server response is parsed then converted to an object, typically [AgroImagery].
172+
///
173+
/// - Parameter options: the request options parameters
174+
/// - Returns: return a AnyPublisher<T?, AgroAPIError>
175+
public func fetchThis<T: Decodable>(options: AgroOptions) -> AnyPublisher<T?, AgroAPIError> {
176+
guard let url = urlSatBuilder(options: options) else {
177+
return Just<T?>(nil).setFailureType(to: AgroAPIError.self).eraseToAnyPublisher()
178+
}
162179
var request = URLRequest(url: url)
163180
request.httpMethod = "GET"
164181
request.addValue(mediaType, forHTTPHeaderField: "Accept")
@@ -201,24 +218,7 @@ public class AgroClient {
201218

202219
return self.doDataTaskPublish(request: request)
203220
}
204-
205-
/// fetch data from the server. A GET request with the chosen options is sent to the server.
206-
/// The server response is parsed then converted to an object, typically [AgroImagery].
207-
///
208-
/// - Parameter options: the request options parameters
209-
/// - Returns: return a AnyPublisher<T?, AgroAPIError>
210-
public func fetchThis<T: Decodable>(options: AgroOptions) -> AnyPublisher<T?, AgroAPIError> {
211-
guard let url = urlSatBuilder(options: options) else {
212-
return Just<T?>(nil).setFailureType(to: AgroAPIError.self).eraseToAnyPublisher()
213-
}
214-
var request = URLRequest(url: url)
215-
request.httpMethod = "GET"
216-
request.addValue(mediaType, forHTTPHeaderField: "Accept")
217-
request.addValue(mediaType, forHTTPHeaderField: "Content-Type")
218-
219-
return self.doDataTaskPublish(request: request)
220-
}
221-
221+
222222
/// fetch data from the server. A GET request with the chosen url is sent to the server.
223223
/// The server response is parsed then converted to an object.
224224
///

Sources/AgroAPI/AgroProtocol.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,14 @@ public class AgroOptions {
176176
return stringer
177177
}
178178

179-
/// to return the option suitable for historical NDVI request
179+
/// returns the options suitable for historical NDVI request
180180
public func toHistoryNDVIParamString() -> String {
181-
var stringer = toParamString()
181+
let stringer = toParamString()
182182
return stringer.replacingOccurrences(of: "polygon_id", with: "polyid")
183183
}
184184
}
185185

186-
/// a server response to a Agro historycal NDVI request
186+
/// the server response to a Agro historycal NDVI request
187187
public struct AgroHistoryNDVI: Codable {
188188
public let dt: Int?
189189
public let source: String?

Sources/AgroAPI/AgroProvider.swift

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ open class AgroProvider {
2424
self.client = AgroClient(apiKey: apiKey)
2525
}
2626

27-
private func postThis(data: Data) -> AnyPublisher<AgroPolyResponse?, AgroAPIError> {
28-
return client.postThis(bodyData: data)
29-
}
30-
3127
/// send an Agro polygon to the server and return the server response
3228
///
3329
/// - Parameter poly: the polygon to send
@@ -46,7 +42,7 @@ open class AgroProvider {
4642
/// - closure completion: AgroPolyResponse
4743
open func createPoly(poly: AgroPolygon, completion: @escaping (AgroPolyResponse?) -> Void) {
4844
let jsonData = (try? JSONEncoder().encode(poly)) ?? Data()
49-
cancellable = postThis(data: jsonData)
45+
cancellable = client.postThis(bodyData: jsonData)
5046
.sink(receiveCompletion: { completion in
5147
switch completion {
5248
case .finished:
@@ -58,11 +54,7 @@ open class AgroProvider {
5854
return completion(resp)
5955
})
6056
}
61-
62-
private func fetchPoly(param: String) -> AnyPublisher<AgroPolyResponse?, AgroAPIError> {
63-
return client.fetchThis(param: param)
64-
}
65-
57+
6658
/// get the specific Agro polygon info from the server
6759
///
6860
/// - Parameter id: the id of the polygon to get
@@ -80,7 +72,7 @@ open class AgroProvider {
8072
/// - Parameter id: the id of the polygon to get
8173
/// - closure completion: AgroPolyResponse
8274
open func getPoly(id: String, completion: @escaping (AgroPolyResponse?) -> Void) {
83-
cancellable = fetchPoly(param: id)
75+
cancellable = client.fetchThis(param: id)
8476
.sink(receiveCompletion: { completion in
8577
switch completion {
8678
case .finished:
@@ -93,10 +85,6 @@ open class AgroProvider {
9385
})
9486
}
9587

96-
private func fetchPolyList(param: String) -> AnyPublisher<[AgroPolyResponse]?, AgroAPIError> {
97-
return client.fetchThis(param: param)
98-
}
99-
10088
/// get the Agro polygon list from the server
10189
///
10290
/// - Binding reponse: AgroPolyResponse
@@ -112,7 +100,7 @@ open class AgroProvider {
112100
///
113101
/// - closure completion: [AgroPolyResponse]
114102
open func getPolyList(completion: @escaping ([AgroPolyResponse]?) -> Void) {
115-
cancellable = fetchPolyList(param: "")
103+
cancellable = client.fetchThis(param: "")
116104
.sink(receiveCompletion: { completion in
117105
switch completion {
118106
case .finished:
@@ -125,10 +113,6 @@ open class AgroProvider {
125113
})
126114
}
127115

128-
private func deleteThis(param: String) -> AnyPublisher<AgroPolyResponse?, AgroAPIError> {
129-
return client.deleteThis(param: param)
130-
}
131-
132116
/// delete the specific Agro polygon from the server
133117
///
134118
/// - Parameter id: the id of the polygon to delete
@@ -146,7 +130,7 @@ open class AgroProvider {
146130
/// - Parameter id: the id of the polygon to delete
147131
/// - closure completion: AgroPolyResponse
148132
open func deletePoly(id: String, completion: @escaping (AgroPolyResponse?) -> Void) {
149-
cancellable = deleteThis(param: id)
133+
cancellable = client.deleteThis(param: id)
150134
.sink(receiveCompletion: { completion in
151135
switch completion {
152136
case .finished:
@@ -204,11 +188,7 @@ open class AgroProvider {
204188
return completion(resp)
205189
})
206190
}
207-
208-
private func fetchImagery(options: AgroOptions) -> AnyPublisher<[AgroImagery]?, AgroAPIError> {
209-
return client.fetchThis(options: options)
210-
}
211-
191+
212192
/// get all available satellite imageries for the polygon and return the info
213193
///
214194
/// - Parameter options: the options
@@ -226,7 +206,7 @@ open class AgroProvider {
226206
/// - Parameter options: the options
227207
/// - closure completion: [AgroImagery]
228208
open func getImagery(options: AgroOptions, completion: @escaping ([AgroImagery]?) -> Void) {
229-
cancellable = fetchImagery(options: options)
209+
cancellable = client.fetchThis(options: options)
230210
.sink(receiveCompletion: { completion in
231211
switch completion {
232212
case .finished:
@@ -239,10 +219,6 @@ open class AgroProvider {
239219
})
240220
}
241221

242-
private func fetchStatsInfo(urlString: String) -> AnyPublisher<AgroStatsInfo?, AgroAPIError> {
243-
return client.fetchThisUrl(urlString: urlString)
244-
}
245-
246222
/// get the satellite imageries stats for the polygon
247223
///
248224
/// - Parameter urlString: the url to fetch
@@ -260,7 +236,7 @@ open class AgroProvider {
260236
/// - Parameter urlString: the url to fetch
261237
/// - closure completion: AgroStatsInfo
262238
open func getStatsInfo(urlString: String, completion: @escaping (AgroStatsInfo?) -> Void) {
263-
cancellable = fetchStatsInfo(urlString: urlString)
239+
cancellable = client.fetchThisUrl(urlString: urlString)
264240
.sink(receiveCompletion: { completion in
265241
switch completion {
266242
case .finished:
@@ -272,11 +248,7 @@ open class AgroProvider {
272248
return completion(resp)
273249
})
274250
}
275-
276-
private func fetchThisData(urlString: String) -> AnyPublisher<Data?, AgroAPIError> {
277-
return client.fetchThisData(urlString: urlString)
278-
}
279-
251+
280252
/// get the satellite imageries tile data for the polygon
281253
///
282254
/// - Parameter urlString: the url to fetch
@@ -294,7 +266,7 @@ open class AgroProvider {
294266
/// - Parameter urlString: the url to fetch
295267
/// - closure completion: Data
296268
open func getTile(urlString: String, completion: @escaping (Data?) -> Void) {
297-
cancellable = fetchThisData(urlString: urlString)
269+
cancellable = client.fetchThisData(urlString: urlString)
298270
.sink(receiveCompletion: { completion in
299271
switch completion {
300272
case .finished:
@@ -325,7 +297,7 @@ open class AgroProvider {
325297
/// - closure completion: Data
326298
open func getPngImageData(urlString: String, paletteid: Int, completion: @escaping (Data?) -> Void) {
327299
let theUrl = urlString + "&paletteid=\(paletteid)"
328-
cancellable = fetchThisData(urlString: theUrl)
300+
cancellable = client.fetchThisData(urlString: theUrl)
329301
.sink(receiveCompletion: { completion in
330302
switch completion {
331303
case .finished:
@@ -409,16 +381,12 @@ open class AgroProvider {
409381
}
410382
}
411383

412-
private func fetchCurrentWeather(param: String) -> AnyPublisher<Current?, AgroAPIError> {
413-
return client.fetchThisWeather(param: param, isForecast: false)
414-
}
415-
416384
/// get the current weather for the polygon
417385
///
418386
/// - Parameter param: the polygon id
419387
/// - closure completion: Current weather
420388
open func getCurrentWeather(id: String, completion: @escaping (Current?) -> Void) {
421-
cancellable = fetchCurrentWeather(param: id)
389+
cancellable = client.fetchThisWeather(param: id, isForecast: false)
422390
.sink(receiveCompletion: { completion in
423391
switch completion {
424392
case .finished:
@@ -442,17 +410,13 @@ open class AgroProvider {
442410
}
443411
}
444412
}
445-
446-
private func fetchWeather(param: String, options: WeatherOptions? = nil) -> AnyPublisher<[Current]?, AgroAPIError> {
447-
return client.fetchThisWeather(param: param, isForecast: true, options: options)
448-
}
449-
413+
450414
/// get the forecast weather for the polygon
451415
///
452416
/// - Parameter param: the polygon id
453417
/// - closure completion: Current weather
454418
open func getForecastWeather(id: String, completion: @escaping ([Current]?) -> Void) {
455-
cancellable = fetchWeather(param: id)
419+
cancellable = client.fetchThisWeather(param: id, isForecast: true)
456420
.sink(receiveCompletion: { completion in
457421
switch completion {
458422
case .finished:
@@ -478,7 +442,7 @@ open class AgroProvider {
478442
/// - Parameter param: the polygon id
479443
/// - closure completion: Current weather
480444
open func getHistoricalWeather(options: WeatherOptions, completion: @escaping ([Current]?) -> Void) {
481-
cancellable = fetchWeather(param: options.polygon_id, options: options)
445+
cancellable = client.fetchThisWeather(param: options.polygon_id, isForecast: true, options: options)
482446
.sink(receiveCompletion: { completion in
483447
switch completion {
484448
case .finished:
@@ -498,17 +462,13 @@ open class AgroProvider {
498462
}
499463
}
500464
}
501-
502-
private func fetchHistory(options: AgroOptions) -> AnyPublisher<[AgroHistoryNDVI]?, AgroAPIError> {
503-
return client.fetchThisHistory(options: options)
504-
}
505-
465+
506466
/// get the historical NDVI for the polygon
507467
///
508468
/// - Parameter options: options for the request
509469
/// - closure completion: historical NDVI
510470
open func getHistoricalNDVI(options: AgroOptions, completion: @escaping ([AgroHistoryNDVI]?) -> Void) {
511-
cancellable = fetchHistory(options: options)
471+
cancellable = client.fetchThisHistory(options: options)
512472
.sink(receiveCompletion: { completion in
513473
switch completion {
514474
case .finished:

Sources/AgroAPI/AgroWeather.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public struct MainData: Codable {
3030
public let temp_kf: Double?
3131
}
3232

33-
// MARK: - Current
3433
public struct Current: Codable {
3534
public let dt: Int
3635
public let main: MainData?
@@ -55,7 +54,6 @@ public struct Current: Codable {
5554
}
5655
}
5756

58-
// MARK: - Rain
5957
public struct Rain: Codable {
6058
public let the1H: Double?
6159
public let the3H: Double?
@@ -82,7 +80,6 @@ public struct Rain: Codable {
8280

8381
}
8482

85-
// MARK: - Snow
8683
public struct Snow: Codable {
8784
public let the1H: Double?
8885
public let the3H: Double?
@@ -108,7 +105,6 @@ public struct Snow: Codable {
108105
}
109106
}
110107

111-
// MARK: - Weather
112108
public struct Weather: Codable {
113109
public let id: Int
114110
public let main, weatherDescription, icon: String

0 commit comments

Comments
 (0)