-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11-api-reference.Rmd
More file actions
572 lines (475 loc) · 16.9 KB
/
Copy path11-api-reference.Rmd
File metadata and controls
572 lines (475 loc) · 16.9 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(eval = FALSE)
```
# API Reference
<div class="info-box">
<strong>Base URL:</strong> <code>https://api.internetofwildlife.com</code>
<br>
<strong>Protocol:</strong> HTTPS
<br>
<strong>Method:</strong> All endpoints use POST
<br>
<strong>Encoding:</strong> JSON
<br>
<strong>Timeout:</strong> 60 seconds per request
</div>
All endpoints require an API token passed in the JSON request body as `token`. The token is loaded from a `.env` file via `dotenv` and accessed with `Sys.getenv("MY_TOKEN")`.
```{r}
library(dotenv)
load_dot_env()
my_token <- Sys.getenv("MY_TOKEN")
```
## Typical Workflow
```
1. Authenticate load_dot_env() -> Sys.getenv("MY_TOKEN")
2. List projects POST /station/api/projects
3. List stations POST /station/api/stations/ (per project)
4. List files POST /station/api/file-list (per station, paginated)
5. Download files POST /station/api/download-file/ (per file)
6. (Optional) Import file_handle() -> db_prep() -> db_insert()
```
The `get_my_data()` function orchestrates this entire workflow in a single call.
## POST Wrapper
All endpoints (except direct calls in `get_my_projects` and `project_list`) go through the `post()` function in `R/post.R`:
```{r}
post(endpoint, payload = NULL, show_progress = FALSE)
```
* Automatically injects `token = my_token` into every request
* 60-second timeout on all requests
* Detects HTTP 503 and emits a warning suggesting narrower date ranges or pagination
* Calls `httr::stop_for_status()` to raise on other HTTP errors
* Optional progress bar via `httr::progress()`
<hr class="section-divider">
## List Projects <span class="tag-label tag-projects">Projects</span>
<div class="endpoint-header">
<span class="method-badge method-post">POST</span>
<span class="endpoint-path">/station/api/projects</span>
</div>
Retrieves all projects associated with the authenticated user's account.
### Parameters
<table class="param-table">
<thead>
<tr><th>Field</th><th>Type</th><th>Required</th><th>Description</th></tr>
</thead>
<tbody>
<tr>
<td><code>token</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>API authentication token</td>
</tr>
</tbody>
</table>
### curl Example
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/projects \
-H "Content-Type: application/json" \
-d '{"token": "YOUR_API_TOKEN"}'
```
### Response
<div class="response-section">
**200 OK**
```json
{
"projects": [
{
"id": "abc123",
"name": "My Wildlife Project"
},
{
"id": "def456",
"name": "Raptor Tracking Study"
}
]
}
```
</div>
### Errors
<table class="error-table">
<thead>
<tr><th>Status</th><th>Cause</th><th>What You See</th></tr>
</thead>
<tbody>
<tr>
<td><span class="error-code">401</span></td>
<td>Invalid or expired API token</td>
<td><code>httr::stop_for_status()</code> raises: <code>"Unauthorized (HTTP 401)"</code></td>
</tr>
<tr>
<td><span class="error-code">503</span></td>
<td>Server overloaded or temporarily down</td>
<td>Warning: <code>"Server query timeout — try narrowing your date range or using pagination"</code>, then execution stops</td>
</tr>
<tr>
<td><span class="error-code">Timeout</span></td>
<td>Network issue or server unreachable</td>
<td>R error: <code>"Timeout was reached"</code> after 60 seconds</td>
</tr>
<tr>
<td><span class="error-code">Empty token</span></td>
<td><code>.env</code> file missing or <code>MY_TOKEN</code> not set</td>
<td>API returns auth error; <code>Sys.getenv("MY_TOKEN")</code> silently returns <code>""</code> if unset</td>
</tr>
<tr>
<td><span class="error-code">Not found</span></td>
<td>Specified <code>myproject</code> name doesn't match any project</td>
<td><code>"The project you entered is not found in your project list. Check your spelling and if you have access to the project."</code></td>
</tr>
</tbody>
</table>
### R Package Functions
<div class="used-by">
* <code>get_my_projects()</code> in <code>R/get_my_projects.R</code> — returns a character vector of project names
* <code>project_list()</code> in <code>R/project_list.R</code> — returns filtered project list with optional name matching
</div>
<hr class="section-divider">
## List Stations <span class="tag-label tag-stations">Stations</span>
<div class="endpoint-header">
<span class="method-badge method-post">POST</span>
<span class="endpoint-path">/station/api/stations/</span>
</div>
Retrieves all sensor stations deployed under a given project.
### Parameters
<table class="param-table">
<thead>
<tr><th>Field</th><th>Type</th><th>Required</th><th>Description</th></tr>
</thead>
<tbody>
<tr>
<td><code>token</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>API authentication token</td>
</tr>
<tr>
<td><code>project-id</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>Project identifier obtained from the List Projects endpoint</td>
</tr>
</tbody>
</table>
### curl Example
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/stations/ \
-H "Content-Type: application/json" \
-d '{"token": "YOUR_API_TOKEN", "project-id": "YOUR_PROJECT_ID"}'
```
### Response
<div class="response-section">
**200 OK**
```json
{
"stations": [
{
"station": {
"id": "STATION-001"
},
"deploy-at": "2024-01-15T00:00:00.000Z",
"end-at": "2024-06-01T00:00:00.000Z"
},
{
"station": {
"id": "STATION-002"
},
"deploy-at": "2024-03-01T00:00:00.000Z",
"end-at": null
}
]
}
```
Each station entry includes:
* **`station.id`** — unique station identifier
* **`deploy-at`** — ISO 8601 deployment start timestamp
* **`end-at`** — ISO 8601 deployment end timestamp (null for active stations)
</div>
### Errors
<table class="error-table">
<thead>
<tr><th>Status</th><th>Cause</th><th>What You See</th></tr>
</thead>
<tbody>
<tr>
<td><span class="error-code">401</span></td>
<td>Invalid or expired API token</td>
<td><code>httr::stop_for_status()</code> raises: <code>"Unauthorized (HTTP 401)"</code></td>
</tr>
<tr>
<td><span class="error-code">404</span></td>
<td><code>project-id</code> does not exist or user lacks access</td>
<td><code>httr::stop_for_status()</code> stops execution with an HTTP error</td>
</tr>
<tr>
<td><span class="error-code">503</span></td>
<td>Server overloaded or temporarily down</td>
<td>Warning: <code>"Server query timeout — try narrowing your date range or using pagination"</code>, then execution stops</td>
</tr>
<tr>
<td><span class="error-code">Timeout</span></td>
<td>Network issue or server unreachable</td>
<td>R error: <code>"Timeout was reached"</code> after 60 seconds</td>
</tr>
<tr>
<td><span class="error-code">Empty list</span></td>
<td>Project exists but has no deployed stations</td>
<td>Response returns <code>"stations": []</code>; downstream code in <code>get_data()</code> skips processing</td>
</tr>
</tbody>
</table>
### R Package Functions
<div class="used-by">
* <code>get_stations()</code> in <code>R/get_stations.R</code>
* <code>get_data()</code> in <code>R/get_data.R</code> — iterates stations to build file lists
</div>
<hr class="section-divider">
## List Station Files <span class="tag-label tag-files">Files</span>
<div class="endpoint-header">
<span class="method-badge method-post">POST</span>
<span class="endpoint-path">/station/api/file-list</span>
</div>
Retrieves metadata for data files available from a specific station, with optional date range and file type filtering. Supports pagination via `limit` and `offset`.
### Parameters
<table class="param-table">
<thead>
<tr><th>Field</th><th>Type</th><th>Required</th><th>Description</th></tr>
</thead>
<tbody>
<tr>
<td><code>token</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>API authentication token</td>
</tr>
<tr>
<td><code>station-id</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>Station identifier obtained from the List Stations endpoint</td>
</tr>
<tr>
<td><code>begin</code></td>
<td><span class="param-type">date</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>Start date for file search (ISO date format, e.g. <code>2024-01-01</code>)</td>
</tr>
<tr>
<td><code>end</code></td>
<td><span class="param-type">date</span></td>
<td><span class="param-optional">optional</span></td>
<td>End date for file search. Defaults to today if omitted.</td>
</tr>
<tr>
<td><code>file-types</code></td>
<td><span class="param-type">string[]</span></td>
<td><span class="param-optional">optional</span></td>
<td>Filter by file types: <code>data</code>, <code>node-data</code>, <code>gps</code>, <code>log</code>, <code>telemetry</code>, <code>sensorgnome</code>, <code>ble</code>, <code>blu</code></td>
</tr>
<tr>
<td><code>limit</code></td>
<td><span class="param-type">integer</span></td>
<td><span class="param-optional">optional</span></td>
<td>Maximum number of files to return (for pagination)</td>
</tr>
<tr>
<td><code>offset</code></td>
<td><span class="param-type">integer</span></td>
<td><span class="param-optional">optional</span></td>
<td>Number of files to skip (for pagination)</td>
</tr>
</tbody>
</table>
### curl Examples
**Basic request (required fields only):**
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/file-list \
-H "Content-Type: application/json" \
-d '{"token": "YOUR_API_TOKEN", "station-id": "YOUR_STATION_ID", "begin": "2024-01-01"}'
```
**Full request with all options:**
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/file-list \
-H "Content-Type: application/json" \
-d '{
"token": "YOUR_API_TOKEN",
"station-id": "YOUR_STATION_ID",
"begin": "2024-01-01",
"end": "2024-02-01",
"file-types": ["data", "gps"],
"limit": 100,
"offset": 0
}'
```
### Response
<div class="response-section">
**200 OK**
```json
{
"files": [
{
"id": "file-789",
"name": "CTT-STATION001-data.csv.gz"
},
{
"id": "file-790",
"name": "CTT-STATION001-gps.csv.gz"
}
]
}
```
</div>
<div class="info-box">
<strong>Note:</strong> Large date ranges may cause 503 server timeouts. Use <code>get_station_file_list_paginated()</code> to handle this automatically. The paginated version retries failed chunks up to <code>max_retries</code> times (default 3) with exponential backoff (2s, 4s, 6s).
</div>
### Errors
<table class="error-table">
<thead>
<tr><th>Status</th><th>Cause</th><th>What You See</th></tr>
</thead>
<tbody>
<tr>
<td><span class="error-code">401</span></td>
<td>Invalid or expired API token</td>
<td><code>httr::stop_for_status()</code> raises: <code>"Unauthorized (HTTP 401)"</code></td>
</tr>
<tr>
<td><span class="error-code">503</span></td>
<td>Date range too large; server times out</td>
<td>Warning: <code>"Server query timeout — try narrowing your date range or using pagination"</code>. The paginated wrapper catches this and retries: <code>"Request timed out, retrying (attempt X of Y)..."</code></td>
</tr>
<tr>
<td><span class="error-code">Timeout</span></td>
<td>Network issue or server unreachable</td>
<td>R error: <code>"Timeout was reached"</code> after 60 seconds. Paginated wrapper retries up to <code>max_retries</code> times with exponential backoff.</td>
</tr>
<tr>
<td><span class="error-code">Invalid type</span></td>
<td>A value in <code>file-types</code> is not one of the 8 valid types</td>
<td>Warning: <code>"WARNING: invalid file type specified - ignoring: <type>"</code>. Invalid types are dropped; request proceeds with valid types only.</td>
</tr>
<tr>
<td><span class="error-code">404</span></td>
<td><code>station-id</code> does not exist</td>
<td><code>httr::stop_for_status()</code> stops execution with an HTTP error</td>
</tr>
<tr>
<td><span class="error-code">Empty result</span></td>
<td>Station has no files for the given date range</td>
<td><code>get_data()</code> prints <code>"no files found from API"</code> and returns an empty list</td>
</tr>
<tr>
<td><span class="error-code">Max retries</span></td>
<td>Paginated wrapper exhausts all retry attempts on a chunk</td>
<td>The original error is re-thrown via <code>stop(e)</code>, halting execution</td>
</tr>
</tbody>
</table>
### R Package Functions
<div class="used-by">
* <code>get_station_file_list()</code> in <code>R/get_station_file_list.R</code> — single request with optional pagination params
* <code>get_station_file_list_paginated()</code> in <code>R/get_station_file_list_paginated.R</code> — automatic date-chunked pagination with retry logic
</div>
<hr class="section-divider">
## Download File <span class="tag-label tag-files">Files</span>
<div class="endpoint-header">
<span class="method-badge method-post">POST</span>
<span class="endpoint-path">/station/api/download-file/</span>
</div>
Downloads the contents of a single data file by its ID.
### Parameters
<table class="param-table">
<thead>
<tr><th>Field</th><th>Type</th><th>Required</th><th>Description</th></tr>
</thead>
<tbody>
<tr>
<td><code>token</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>API authentication token</td>
</tr>
<tr>
<td><code>file-id</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-required">REQUIRED</span></td>
<td>File identifier obtained from the List Station Files endpoint</td>
</tr>
<tr>
<td><code>bypass-encoding</code></td>
<td><span class="param-type">string</span></td>
<td><span class="param-optional">optional</span></td>
<td>Set to <code>"plain"</code> to bypass encoding. Used automatically as a fallback on retry.</td>
</tr>
</tbody>
</table>
### curl Examples
**Basic download:**
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/download-file/ \
-H "Content-Type: application/json" \
-d '{"token": "YOUR_API_TOKEN", "file-id": "YOUR_FILE_ID"}'
```
**With bypass encoding (fallback on error):**
```{bash}
curl -X POST https://api.internetofwildlife.com/station/api/download-file/ \
-H "Content-Type: application/json" \
-d '{"token": "YOUR_API_TOKEN", "file-id": "YOUR_FILE_ID", "bypass-encoding": "plain"}'
```
### Response
<div class="response-section">
**200 OK** — Raw file content (CSV text, UTF-8 encoded)
```
TagId,NodeId,TagRSSI,Validated,Time
TAG-001,NODE-A,-70,1,2024-01-15 12:00:00
TAG-001,NODE-B,-85,1,2024-01-15 12:00:00
```
In batch downloads via `get_data()`, each file result is tracked in a dataframe. The final summary prints: `"Download complete: X succeeded, Y failed out of Z files"`
</div>
### Errors
<table class="error-table">
<thead>
<tr><th>Status</th><th>Cause</th><th>What You See</th></tr>
</thead>
<tbody>
<tr>
<td><span class="error-code">401</span></td>
<td>Invalid or expired API token</td>
<td><code>httr::stop_for_status()</code> raises: <code>"Unauthorized (HTTP 401)"</code></td>
</tr>
<tr>
<td><span class="error-code">404</span></td>
<td><code>file-id</code> does not match any file on the server</td>
<td><code>httr::stop_for_status()</code> stops execution with an HTTP error</td>
</tr>
<tr>
<td><span class="error-code">500</span></td>
<td>File content has encoding issues the server can't handle</td>
<td><code>download_files()</code> prints <code>"Here's the original error message:"</code> followed by the error detail, then automatically retries with <code>bypass-encoding = "plain"</code></td>
</tr>
<tr>
<td><span class="error-code">500 (retry)</span></td>
<td>File is corrupted or unrecoverable (retry also fails)</td>
<td>Error propagates up; <code>get_data()</code> catches it per-file and records <code>status = "failed"</code> in results dataframe</td>
</tr>
<tr>
<td><span class="error-code">503</span></td>
<td>Server overloaded or temporarily down</td>
<td>Warning: <code>"Server query timeout — try narrowing your date range or using pagination"</code>, then execution stops</td>
</tr>
<tr>
<td><span class="error-code">Timeout</span></td>
<td>Network issue or server unreachable</td>
<td>R error: <code>"Timeout was reached"</code> after 60 seconds</td>
</tr>
<tr>
<td><span class="error-code">Batch failure</span></td>
<td>Any error during a single file download within <code>get_data()</code></td>
<td>File is marked <code>"failed"</code> in the results dataframe; remaining files continue. Final summary: <code>"Download complete: X succeeded, Y failed out of Z files"</code></td>
</tr>
</tbody>
</table>
### R Package Functions
<div class="used-by">
* <code>download_files()</code> in <code>R/download_files.R</code>
* Called from <code>get_data()</code> in <code>R/get_data.R</code> for each file in the download queue
</div>