6
6
turbasen.py
7
7
=============================
8
8
9
- Python client for `Nasjonal Turbase <http://www.nasjonalturbase.no/ >`_.
10
-
11
- This client is opinionated about data fields and unrecognized fields require
12
- explicit handling if wanted.
9
+ Python client for `Nasjonal Turbase <http://www.nasjonalturbase.no/ >`_,
10
+ featuring:
11
+
12
+ - :ref: `Object model for all datatypes <datatypes >`
13
+ - :ref: `Container API on instances to access document fields (dict-like)
14
+ <document-fields>`
15
+ - :ref: `Exceptions abstract away HTTP status codes <exceptions >`
16
+ - :ref: `Automatic iteration over paginated list queries <static-methods >`
17
+ - :ref: `Handling partial documents returned from list queries
18
+ <partial-documents>`
19
+ - :ref: `ETag handling with refresh on expiry <settings >`
20
+ - :ref: `Client caching <settings >`
21
+ - :ref: `Event triggers <events >`
13
22
14
23
Installation
15
24
-----------------------------
@@ -18,6 +27,7 @@ Installation
18
27
19
28
pip install turbasen
20
29
30
+ .. _datatypes :
21
31
22
32
Datatypes
23
33
-----------------------------
@@ -30,7 +40,7 @@ Datatypes
30
40
31
41
`Groups <http://www.nasjonalturbase.no/data/grupper.html >`_
32
42
33
- .. py :class :: turbasen.Omrade
43
+ .. py :class :: turbasen.Område
34
44
35
45
`Areas <http://www.nasjonalturbase.no/data/omrader.html >`_
36
46
@@ -51,14 +61,16 @@ Environment variables
51
61
``ENDPOINT_URL ``
52
62
API endpoint. See the ``ENDPOINT_URL `` setting.
53
63
64
+ .. _settings :
65
+
54
66
Settings
55
67
-----------------------------
56
68
57
69
``ENDPOINT_URL = https://api.nasjonalturbase.no ``
58
70
API endpoint. Set to ``https://dev.nasjonalturbase.no `` for development.
59
71
60
72
``LIMIT = 20 ``
61
- Objects returned per page. API hard max limit is currently 50. Note that
73
+ Documents returned per page. API hard max limit is currently 50. Note that
62
74
setting this to a low number when the use case is to retrieve all documents is
63
75
inefficient.
64
76
@@ -75,122 +87,135 @@ Settings
75
87
normally be high.
76
88
77
89
``ETAG_CACHE_PERIOD = 60 * 60 ``
78
- Number of seconds to ignore ``Etag `` checks and use local cache blindly.
90
+ Number of seconds to ignore ``ETag `` checks and use local cache blindly.
79
91
80
- ``API_KEY = os.environ.get('API_KEY') ``
81
- API key is currently required for access.
92
+ ``API_KEY = os.environ.get('API_KEY', '') ``
93
+ Get your API key at
94
+ `Nasjonal Turbase Developer <https://developer.nasjonalturbase.no/ >`_.
82
95
83
96
84
97
85
- Usage
98
+ Example usage
86
99
-----------------------------
87
100
101
+ Initialization:
102
+
88
103
.. code-block :: python
89
104
90
- # Initialization
91
105
import turbasen
92
106
turbasen.configure(LIMIT = 3 , ENDPOINT = ' https://dev.nasjonalturbase.no' )
93
107
94
- # Lookup partial documents
95
- turbasen.Sted.lookup(pages = 1 )
96
- # [<Sted: 546b36a511f41a9c00c0d4d9 (partial): En liten hytte>,
97
- # <Sted: 546a051011f41a9c00c0d4cc (partial): Snøhulen>,
98
- # <Sted: 555f1f4206b9ce06003405c5 (partial): Strømfoss>]
108
+ List documents, with some parameter filters:
109
+
110
+ .. code-block :: python
111
+
112
+ turbasen.Sted.list(pages = 1 , params = {
113
+ ' tilbyder' : ' DNT' ,
114
+ ' status' : ' Offentlig' ,
115
+ ' tags' : ' Hytte' ,
116
+ })
99
117
100
- # Add filter parameters
101
- turbasen.Sted.lookup(pages = 1 , params = {' tags' : ' Hytte' })
102
118
# [<Sted: 52407fb375049e561500027d (partial): Øvre Grue>,
103
119
# <Sted: 52407fb375049e561500035a (partial): Ravnastua fjellstue>,
104
120
# <Sted: 52407fb375049e5615000356 (partial): Lahpoluoppal>]
105
121
106
- # Get single document
122
+ Get single document:
123
+
124
+ .. code-block :: python
125
+
107
126
sted = turbasen.Sted.get(' 546b36a511f41a9c00c0d4d9' )
108
127
# <Sted: 546b36a511f41a9c00c0d4d9: En liten hytte>
109
- sted.geojson
110
- # {
111
- # 'coordinates': [8.2912015914917, 60.12502756386393],
112
- # 'type': 'Point'
113
- # }
114
- len (sted.get_data().keys())
115
- # 12
116
128
117
- # Save document
118
- sted.save()
129
+ sted[' navn' ]
130
+ # En liten hytte
131
+
132
+ len (sted)
133
+ # 17
134
+
135
+ Create and delete document:
136
+
137
+ .. code-block :: python
119
138
120
- # Unrecognized fields are discarded by default and require
121
- # explicit handling explicitly if wanted
122
- len (sted.get_data(include_extra = True ).keys())
123
- # 13
124
- {
125
- k: v
126
- for k, v in s.get_data(include_extra = True ).items()
127
- if k not in s.get_data()
128
- }
129
- # {'unknown_key': 'foo'}
130
- sted.save(include_extra = True )
131
-
132
- # Create and delete document
133
139
sted = turbasen.Sted(
134
140
lisens = ' Privat' ,
135
141
status = ' Kladd' ,
136
142
navn = ' Testcabin' ,
137
143
beskrivelse = ' Testcabin' ,
138
144
tags = [' Hytte' ],
139
145
)
146
+
140
147
sted.save()
141
- # Turbasen POST warning: {
142
- # 'resource': 'Document',
143
- # 'field': 'navngiving',
148
+ # API warning: {
144
149
# 'code': 'missing_field',
150
+ # 'field': 'navngiving',
151
+ # 'resource': 'Document'
145
152
# }
153
+
146
154
sted.delete()
147
155
148
156
149
157
API
150
158
-----------------------------
151
159
160
+ .. _static-methods :
161
+
152
162
Static methods
153
163
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154
164
155
- .. py :function :: lookup (pages = None , params = dict ())
165
+ .. py :function :: list (pages = None , params = dict ())
156
166
157
- Return an iterator yielding all objects of this class object type. Limit the
158
- number of objects to the number of ``pages `` wanted where each page contains
159
- ``LIMIT `` objects from the settings.
167
+ Return a list of documents. If ``pages `` is not ``None ``, limits the results
168
+ to ``pages `` pages with ``LIMIT `` documents on each page.
160
169
161
- Parameters passed in the ``params `` dict are forwarded to the API. These may
162
- be used to filter the query, or specify which `` fields `` should be returned
163
- to increase performance, avoiding extra fetches for
164
- :ref: ` partial objects < partial-objects >` .
170
+ Filter results with ``params ``, or specify which `` fields `` should be
171
+ returned to increase performance, avoiding extra fetches for
172
+ :ref: ` partial documents < partial-documents >`. See
173
+ ` the API documentation < http://www.nasjonalturbase.no/api/ >`_ .
165
174
166
175
.. py :function :: get(object_id)
167
176
168
- Retrieve a document of this class object type. Raises
169
- ``turbasen.exceptions.DocumentNotFound `` if the document doesn't exist.
177
+ Retrieve a document of this datatype with the given object id.
178
+
179
+ .. _instance-methods :
170
180
171
181
Instance methods
172
182
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173
183
174
- .. py :function :: get_data(self , include_common = True , include_extra = False )
184
+ .. py :function :: save()
185
+
186
+ Save this document. If the document doesn't have an ``_id `` field, it will be
187
+ assigned.
175
188
176
- Return a dictionary of all data in this document.
189
+ .. py : function :: delete()
177
190
178
- Set ``include_common `` to ``False `` to exclude fields that are common for all
179
- objects, returning only fields specific to the current object type.
191
+ Delete this document. It must be saved (ie. have an ``_id `` field).
180
192
181
- Set `` include_extra `` to `` True `` to include unrecognized fields.
193
+ .. py : function :: get_field(key[, default])
182
194
183
- .. py : function :: delete()
195
+ See ` dict.get < https://docs.python.org/3/library/stdtypes.html?#dict.get >`_
184
196
185
- Delete the current object. It must be saved (ie. have an ``object_id ``).
197
+ .. _document-fields :
198
+
199
+ Document fields
200
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201
+
202
+ Instances are
203
+ `collections <https://docs.python.org/3/library/collections.html >`_, so document
204
+ fields are accessed as keys on a regular ``dict ``. All
205
+ `dict methods <https://docs.python.org/3/library/stdtypes.html?#dict >`_ are
206
+ implemented, except for
207
+ `dict.get <https://docs.python.org/3/library/stdtypes.html?#dict.get >`_ which is
208
+ renamed to ``get_field ``, see :ref: `instance methods <instance-methods >`.
209
+
210
+ .. _exceptions :
186
211
187
212
Exceptions
188
213
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189
214
190
215
.. py :class :: turbasen.exceptions.DocumentNotFound
191
216
192
- Thrown when a `` GET `` request for a document with a given object id isn 't
193
- found
217
+ Thrown when a request references to a document with an object id that doesn 't
218
+ exist.
194
219
195
220
.. py :class :: turbasen.exceptions.Unauthorized
196
221
@@ -201,21 +226,26 @@ Exceptions
201
226
202
227
Thrown when updating or creating a document with invalid data.
203
228
204
- .. _ partial-objects :
229
+ .. py : class :: turbasen.exceptions.ServerError
205
230
206
- Partial objects
231
+ Thrown when a request results in a 5xx server error response.
232
+
233
+ .. _partial-documents :
234
+
235
+ Partial documents
207
236
-----------------------------
208
237
209
- When using ``lookup ``, not all document data is retrieved. The objects returned
210
- are classified as *partial *. On attribute lookup, if the attribute doesn't
211
- exist, a ``GET `` request is automatically performed under the hood to request
212
- the entire document, and if the attribute is found on the complete object, it is
213
- returned as normal.
238
+ Documents returned from calling ``list `` are not complete, but classified as
239
+ *partial *. When accessing a field on a partial document which does not exist,
240
+ a ``GET `` request is automatically performed under the hood to request the
241
+ entire document. If the accessed field now exists, it is returned as normal.
214
242
215
- If you know you only need a few fields from a lookup , it may be a good idea to
216
- specify those in the params field like this:
243
+ If you know you only need a few fields from a `` list `` call , it may be a good
244
+ idea to specify those in the params field like this:
217
245
``params={'fields': ['field1', 'field2']} `` to avoid performing a ``GET ``
218
- request for each of the objects in your list.
246
+ request for each of the documents in your list.
247
+
248
+ .. _events :
219
249
220
250
Events
221
251
-----------------------------
0 commit comments