-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupdate.R
More file actions
executable file
·243 lines (206 loc) · 9.38 KB
/
Copy pathupdate.R
File metadata and controls
executable file
·243 lines (206 loc) · 9.38 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' @title Update Field
#'
#' @description
#' \code{update_field} To update details (Farm ID or FieldName) of a particular location in the aWhere API.
#'
#' @details
#' Fields are the easiest way to manage locations in the aWhere APIs, providing an easy reference
#' for tracking weather, agronomics, models, and progress over growing seasons. Once a field is
#' registered, plantings can also be registered for that field with specific information about the
#' crop planted at that location, the date of planting, and other optional information.
#'
#' Occasionally, you may need to update the details of your field. At this time, only the farm ID,
#' field Name, and number of acres can be updated using this function. Field details can only be
#' updated one variable at a time, for one field at a time. If you need to update multiple fields or
#' multiple variables associated with a field, please pass commands sequentially.
#'
#' @param field_id the unique field ID for the field you want to update (character string) (required)
#' @param variable_update the variable that needs to be updated, either "farmId", "name", or "acres"
#' (character string) (required)
#' @param value_update the new value for variable_update, to replace the existing value. The existing
#' value can be found using get_fields("field_id") (character string) (required)
#' @param keyToUse aWhere API key to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param secretToUse aWhere API secret to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param tokenToUse aWhere API token to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param apiAddressToUse Address of aWhere API to use. For advanced use only. Most users will not need to use this parameter (optional)
#'
#' @return - A message confirming the changes have been made
#'
#' @references https://docs.awhere.com/knowledge-base-docs/update-a-field-location/
#'
#' @import httr
#'
#' @examples
#' \dontrun{update_field(field_id = 'field_test',variable_update = 'farmId', value_update = 'This is my territory')}
#' @export
update_field <- function(field_id
,variable_update
,value_update
,keyToUse = awhereEnv75247$uid
,secretToUse = awhereEnv75247$secret
,tokenToUse = awhereEnv75247$token
,apiAddressToUse = awhereEnv75247$apiAddress) {
checkCredentials(keyToUse,secretToUse,tokenToUse)
checkValidField(field_id,keyToUse,secretToUse,tokenToUse)
## Creating the request
url <- paste0(apiAddressToUse, "/fields/",field_id)
postbody <- paste0('[{"op":"replace","path":"/', variable_update, '","value":"', value_update, '"}]')
doWeatherGet <- TRUE
tryCount <- 0
while (doWeatherGet == TRUE) {
tryCount <- tryCount + 1
request <- httr::PATCH(url, body = postbody, httr::content_type('application/json'),
httr:: add_headers(Authorization = paste0("Bearer ", tokenToUse)))
# Re formating the response recieved from API
a <- suppressMessages(httr::content(request, as = "text"))
temp <- check_JSON(a
,request
,tryCount
,keyToUse
,secretToUse
,tokenToUse)
doWeatherGet <- temp[[1]]
#if the token was updated, this will cause it to be used through function
tokenToUse <- temp[[3]]
}
cat(paste0('Operation Complete'))
}
#' @title Update Planting
#'
#' @description
#' \code{update_planting} To update details of a particular planting
#'
#' @details
#' Occasionally you will need to update a planting, changing the projections
#' or recording the end-of-season information for historical tracking and model tuning.
#' This API supports both partial and complete updates of plantings. When updating an
#' entire planting, the whole object is replaced in the database. Any properties that
#' were previously set, and now are not, will be null'd. The required properties must
#' also be set even if they are changed.
#'
#' @param planting_id ID of planting to update
#' @param field_id ID of field to search for plantings within
#' @param planting_date new date to update as planting's plant date
#' @param proj_yield_amount new amount to update as planting's projected yield amount
#' @param proj_yield_units new units to update as planting's projected yield units
#' @param proj_harvest_date new projected harvest date to update as planting's projected harvest date
#' @param yield_amount new amount to update as planting's yield amount
#' @param yield_units new units to update as planting's yield units
#' @param harvest_date new actual harvest date to update as planting's harvest date
#' @param keyToUse aWhere API key to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param secretToUse aWhere API secret to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param tokenToUse aWhere API token to use. For advanced use only. Most users will not need to use this parameter (optional)
#' @param apiAddressToUse Address of aWhere API to use. For advanced use only. Most users will not need to use this parameter (optional)
#'
#' @return - A message confirming the changes have been made
#'
#' @references https://docs.awhere.com/knowledge-base-docs/update-all-or-part-of-a-planting-in-a-field/
#'
#' @import httr
#'
#' @examples
#' \dontrun{update_planting("field_test", "156036", harvest_date = "2016-02-01", yield_amount = "60", yield_units = "Bushels")}
#'
#' @export
update_planting <- function(field_id
,planting_id
,planting_date = ""
,proj_yield_amount = ""
,proj_yield_units = ""
,proj_harvest_date = ""
,yield_amount = ""
,yield_units = ""
,harvest_date = ""
,keyToUse = awhereEnv75247$uid
,secretToUse = awhereEnv75247$secret
,tokenToUse = awhereEnv75247$token
,apiAddressToUse = awhereEnv75247$apiAddress) {
checkCredentials(keyToUse,secretToUse,tokenToUse)
checkValidField(field_id,keyToUse,secretToUse,tokenToUse)
## Error checking
if(field_id == "") {
stop("Field ID is required")
}
if(proj_yield_amount != "" & is.na(as.double(proj_yield_amount))) {
stop("Yield amounts must be numeric")
}
if(yield_amount != "" & is.na(as.double(yield_amount))) {
stop("Yield amounts must be numeric")
}
if(planting_date != "" & !is.Date(ymd(planting_date))){
stop("Dates must be in 'YYYY-MM-DD' format")
}
if(proj_harvest_date != "" & !is.Date(ymd(proj_harvest_date))){
stop("Dates must be in 'YYYY-MM-DD' format")
}
if(harvest_date != "" & !is.Date(ymd(harvest_date))){
stop("Dates must be in 'YYYY-MM-DD' format")
}
## Create postbody
postbody <- c()
i = 0
if(planting_date != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/plantingDate","value":"', planting_date, '"}')
}
if(proj_yield_amount != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/projections/yield/amount","value":', proj_yield_amount, '}')
}
if(proj_yield_units != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/projections/yield/units","value":"', proj_yield_units, '"}')
}
if(proj_harvest_date != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/projections/harvestDate","value":"', proj_harvest_date, '"}')
}
if(yield_amount != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/yield/amount","value":', yield_amount, '}')
}
if(yield_units != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/yield/units","value":"', yield_units, '"}')
}
if(harvest_date != "") {
i = i + 1
postbody[i] <- paste0('{"op":"replace","path":"/harvestDate","value":"', harvest_date, '"}')
}
if(i == 0) {
stop("Must pass in a value to update")
}
body <- paste0("[", postbody[1])
if(length(postbody) > 1) {
for(i in 2:length(postbody)) {
body <- paste0(body, ",", postbody[i])
}
}
body <- paste0(body, "]")
## Creating the request
url <- paste0(apiAddressToUse, "/agronomics/fields/", field_id, "/plantings/")
if(planting_id == "") {
url <- paste0(url, "current")
} else {
url <- paste0(url, planting_id)
}
doWeatherGet <- TRUE
tryCount <- 0
while (doWeatherGet == TRUE) {
tryCount <- tryCount + 1
##Send request
request <- httr::PATCH(url, body = body, httr::content_type('application/json'),
httr::add_headers(Authorization = paste0("Bearer ", tokenToUse)))
a <- suppressMessages(httr::content(request, as = "text"))
temp <- check_JSON(a
,request
,tryCount
,keyToUse
,secretToUse
,tokenToUse)
doWeatherGet <- temp[[1]]
#if the token was updated, this will cause it to be used through function
tokenToUse <- temp[[3]]
}
cat(paste0('Operation Complete'))
}