Skip to content

Commit 643e66a

Browse files
committed
Update documentation and start documenting 2.x client methods.
1 parent 97115b8 commit 643e66a

File tree

3 files changed

+546
-278
lines changed

3 files changed

+546
-278
lines changed

README.md

+13-278
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,10 @@ For more information on farmOS, visit [farmOS.org](https://farmOS.org).
1818
- [OAuth Password Credentials](#oauth-password-credentials-most-common)
1919
- [OAuth Authorization Flow](#oauth-authorization-flow-advanced)
2020
- [Saving OAuth Tokens](#saving-oauth-tokens)
21-
- [Server Info](#server-info)
22-
- [Logs](#logs)
23-
- [`.get()`](#get)
24-
- [`.send()`](#send)
25-
- [`.delete()`](#delete)
26-
- [Assets](#assets)
27-
- [`.get()`](#get-1)
28-
- [`.send()`](#send-1)
29-
- [`.delete()`](#delete-1)
30-
- [Areas](#areas)
31-
- [`.get()`](#get-2)
32-
- [`.send()`](#send-2)
33-
- [`.delete()`](#delete-2)
34-
- [Taxonomy Terms](#taxonomy-terms)
35-
- [`.get()`](#get-3)
36-
- [`.send()`](#send-3)
37-
- [`.delete()`](#delete-3)
21+
- [Server Info](#server-info)
22+
- [Client methods](#client-methods)
23+
- [farmOS 1.x](docs/client_1x.md)
24+
- [farmOS 2.x](docs/client_2x.md)
3825
- [Logging](#logging)
3926

4027

@@ -68,7 +55,8 @@ password = "password"
6855
farm_client = farmOS(
6956
hostname=hostname,
7057
client_id = "farm", # Optional. The default oauth client_id "farm" is enabled on all farmOS servers.
71-
scope="user_access" # Optional. The default scope is "user_access". Only needed if authorizing with a differnt scope.
58+
scope="user_access", # Optional. The default scope is "user_access". Only needed if authorizing with a different scope.
59+
version=1 # Optional. The major version of the farmOS server, 1 or 2. Defaults to 1.
7260
)
7361

7462
# Authorize the client, save the token.
@@ -172,270 +160,17 @@ info = farm_client.info()
172160
}
173161
```
174162

175-
### Logs
176163

164+
### Client methods
177165

178-
A log is any type of event that occurs on the farm, from a planting to a harvest
179-
to just a general observation.
166+
farmOS.py can connect to farmOS servers running version 1.x or 2.x. The version should be specified when instantiating
167+
the farmOS client, see [Authentication](#authentication).
180168

181-
Methods for getting, sending and deleting logs are namespaced on the `farm.log`
182-
property.
169+
Because of [API changes](https://2x.farmos.org/development/api/changes/) in farmOS 2.x, the client provides different
170+
methods depending on the server version:
183171

184-
#### `.get()`
185-
186-
```python
187-
# Get all logs
188-
logs = farm_client.log.get()['list']
189-
190-
# Get harvest logs
191-
filters = {
192-
'type': 'farm_harvest'
193-
}
194-
195-
harvests = farm_client.log.get(filters=filters)['list']
196-
197-
# Get log number 37
198-
log = farm_client.log.get(37)
199-
```
200-
201-
The four default log types are:
202-
203-
- `farm_activity`
204-
- `farm_harvest`
205-
- `farm_input`
206-
- `farm_observation`
207-
208-
Other log types may be provided by add-on modules in farmOS.
209-
210-
#### `.send()`
211-
212-
Send can be used to create a new log, or if the `id` property is included, to
213-
update an existing log:
214-
215-
```python
216-
217-
# Create observation log
218-
observation_log = {
219-
"name": "My Great Planting",
220-
"type": "farm_observation",
221-
"done": 0,
222-
"notes": "Some notes"
223-
}
224-
log = farm_client.log.send(log)
225-
226-
# Mark log 35 as done
227-
done = {
228-
'id': 45,
229-
'done': 1
230-
}
231-
log = farm_client.log.send(done)
232-
```
233-
234-
#### `.delete()`
235-
236-
```python
237-
farm_client.log.delete(123)
238-
```
239-
240-
### Assets
241-
242-
Assets are any piece of property or durable good that belongs to the farm, such
243-
as a piece of equipment, a specific crop, or an animal.
244-
245-
Methods for getting, sending and deleting assets are namespaced on the
246-
`farm.asset` property.
247-
248-
#### `.get()`
249-
250-
```python
251-
252-
# Get all assets
253-
assets = farm_client.asset.get()['list']
254-
255-
# Get all animal assets
256-
filters = {
257-
'type':'animal'
258-
}
259-
animals = farm_client.asset.get(filters=filters)['list']
260-
261-
# Get asset ID 45
262-
asset = farm_client.asset.get(45)
263-
```
264-
265-
Some common asset types include:
266-
267-
- `animal`
268-
- `equipment`
269-
- `planting`
270-
271-
Other asset types may be provided by add-on modules in farmOS.
272-
273-
#### `.send()`
274-
275-
Send can be used to create a new asset, or if the `id` property is included, to update an existing asset:
276-
277-
```python
278-
279-
planting_asset = {
280-
"name": "My Great Planting",
281-
"type": "planting",
282-
"crop": [
283-
{"id": 8} # Crop term id
284-
]
285-
}
286-
287-
asset = farm_client.asset.send(planting_asset)
288-
```
289-
290-
#### `.delete()`
291-
292-
```python
293-
farm_client.asset.delete(123)
294-
```
295-
296-
### Areas
297-
298-
An area is any well defined location that has been mapped in farmOS, such as a field, greenhouse, building, etc.
299-
300-
Here's an example of what an area looks like as a Python dict:
301-
302-
```python
303-
{
304-
'tid': '22',
305-
'name': 'F1',
306-
'description': '',
307-
'area_type': 'greenhouse',
308-
'geofield': [
309-
{
310-
'geom': 'POLYGON ((-75.53640916943549 42.54421203378203, -75.53607389330863 42.54421796218091, -75.53607121109961 42.54415472589722, -75.53640648722647 42.54414682135726, -75.53640916943549 42.54421203378203))',
311-
}
312-
],
313-
'vocabulary': {
314-
'id': '2',
315-
'resource': 'taxonomy_vocabulary'
316-
},
317-
'parent': [
318-
{
319-
'id': 11,
320-
'resource': 'taxonomy_term'
321-
}
322-
],
323-
'weight': '0',
324-
}
325-
```
326-
327-
Methods for getting, sending and deleting areas are namespaced on the `farm.area` property.
328-
329-
#### `.get()`
330-
331-
```python
332-
333-
# Get all areas
334-
areas = farm_client.area.get()['list']
335-
336-
# Get field areas
337-
filters = {
338-
'area_type':'field'
339-
}
340-
fields = farm_client.area.get(filters=filters)['list']
341-
342-
# Get area with tid 37
343-
area = farm_client.area.get(37)
344-
```
345-
346-
__NOTE:__ Areas use a `tid` property, unlike logs and assets which have an `id`. This stands for taxonomy ID. In the future this may be changed to make it more consistent with the other entities.
347-
348-
Some common area types include:
349-
350-
- `field`
351-
- `building`
352-
- `property`
353-
- `water`
354-
- `other`
355-
356-
Other area types may be provided by add-on modules in farmOS.
357-
358-
#### `.send()`
359-
360-
Send can be used to create a new area, or if the `tid` property is included, to update an existing area:
361-
362-
```python
363-
364-
365-
366-
```
367-
368-
#### `.delete()`
369-
370-
```python
371-
farm_client.area.delete(123)
372-
```
373-
374-
### Taxonomy Terms
375-
376-
farmOS allows farmers to build vocabularies of terms for various categorization
377-
purposes. These are referred to as "taxonomies" in farmOS (and Drupal), although
378-
"vocabulary" is sometimes used interchangeably.
379-
380-
Some things that are represented as taxonomy terms include quantity units,
381-
crops/varieties, animal species/breeds, input materials, and log categories.
382-
See "Endpoints" above for specific API endpoints URLs.
383-
384-
A very basic taxonomy term JSON structure looks like this:
385-
386-
```json
387-
{
388-
"tid": "3",
389-
"name": "Cabbage",
390-
"description": "",
391-
"vocabulary": {
392-
"id": "7",
393-
"resource": "taxonomy_vocabulary",
394-
},
395-
"parent": [
396-
{
397-
"id": "10",
398-
"resource": "taxonomy_term",
399-
},
400-
],
401-
"weight": "5",
402-
}
403-
```
404-
405-
The `tid` is the unique ID of the term (database primary key). When creating a
406-
new term, the only required fields are `name` and `vocabulary`. The vocabulary
407-
is an ID that corresponds to the specific vocabulary the term will be a part of
408-
(eg: quantity units, crops/varieties, log categories, etc). The fields `parent`
409-
and `weight` control term hierarchy and ordering (a heavier `weight` will sort
410-
it lower in the list).
411-
412-
#### `.get()`
413-
414-
```python
415-
416-
# Get all terms
417-
terms = farm_client.term.get()['list']
418-
419-
# Get all terms from farm_crops vocabulary
420-
crops = farm_client.term.get('farm_crops')['list']
421-
422-
# Get term ID 67
423-
term = farm_client.term.get(67)
424-
```
425-
426-
#### `.send()`
427-
428-
Send can be used to create a new taxonomy term, or if the `tid` property is included in the term object, to update an existing area:
429-
430-
```python
431-
432-
```
433-
434-
#### `.delete()`
435-
436-
```python
437-
farm_client.term.delete(56)
438-
```
172+
- [1.x methods](docs/client_1x.md)
173+
- [2.x methods](docs/client_2x.md)
439174

440175
### Logging
441176

0 commit comments

Comments
 (0)