-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbexio.py
More file actions
987 lines (776 loc) · 29.3 KB
/
Copy pathbexio.py
File metadata and controls
987 lines (776 loc) · 29.3 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""Interacts with the Bexio API, providing a simplified interface for use with Python."""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026060501'
import urllib.parse
from . import url
# https://docs.bexio.com/
BEXIO_API_BASE_URL = 'https://api.bexio.com'
BEXIO_API_CONTACT_TYPE_COMPANY = 1
BEXIO_API_CONTACT_TYPE_PERSON = 2
BEXIO_API_ACCOUNT_URL = '/2.0/accounts'
BEXIO_API_BANK_ACCOUNT_URL = '/3.0/banking/accounts'
# API endpoint still uses an old name in the URL for "business activities"
BEXIO_API_BUSINESS_ACTIVITY_URL = '/2.0/client_service'
BEXIO_API_CONTACT_URL = '/2.0/contact'
BEXIO_API_CONTACT_GROUP_URL = '/2.0/contact_group'
BEXIO_API_CONTACT_RELATION_URL = '/2.0/contact_relation'
# API endpoint still uses an old name in the URL for "contact sectors"
BEXIO_API_CONTACT_SECTOR_URL = '/2.0/contact_branch'
BEXIO_API_COUNTRY_URL = '/2.0/country'
BEXIO_API_CURRENCY_URL = '/3.0/currencies'
BEXIO_API_DEFAULT_API_CALL_TIMEOUT = 20
BEXIO_API_INVOICE_URL = '/2.0/kb_invoice'
BEXIO_API_ITEM_URL = '/2.0/article' # API endpoint uses a different name in the URL
BEXIO_API_LANGUAGE_URL = '/2.0/language'
BEXIO_API_SALUTATION_URL = '/2.0/salutation'
BEXIO_API_PAYMENT_TYPE_URL = '/2.0/payment_type'
BEXIO_API_PROJECT_STATUS_URL = '/2.0/pr_project_state'
BEXIO_API_PROJECT_TYPE_URL = '/2.0/pr_project_type'
BEXIO_API_PROJECT_URL = '/2.0/pr_project'
BEXIO_API_STOCK_AREA_URL = '/2.0/stock_place'
BEXIO_API_STOCK_LOCATION_URL = '/2.0/stock'
BEXIO_API_TAX_URL = '/3.0/taxes'
BEXIO_API_TIMESHEET_URL = '/2.0/timesheet'
BEXIO_API_TIMESHEET_STATUS_URL = '/2.0/timesheet_status'
BEXIO_API_TITLE_URL = '/2.0/title'
BEXIO_API_UNIT_URL = '/2.0/unit'
BEXIO_API_USER_URL = '/3.0/users'
def call_api(api_token, path, data=None, method=None):
"""
Makes an HTTP GET or POST call against the Bexio API and returns the parsed JSON.
### Parameters
- **api_token** (`str`)
Bexio API Token. Create at https://developer.bexio.com/pat.
- **path** (`str`)
The URL part to call.
- **data** (`dict`, optional)
Dictionary that will be sent as JSON data.
- **method** (`str`, optional)
HTTP method to use for the request. Defaults to GET if data is None else POST.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `list` | `str`):
- On success, the parsed JSON document from the Bexio API
- On failure, an error message string.
"""
if data is None:
data = {}
headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {api_token}',
}
return url.fetch_json(
f'{BEXIO_API_BASE_URL}{path}',
data=data,
header=headers,
timeout=BEXIO_API_DEFAULT_API_CALL_TIMEOUT,
encoding='serialized-json',
method=method,
response_on_error=True,
)
def create_contact(api_token, data=None):
"""
Creates a contact using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`, optional):
Contact data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created contact.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Contacts/operation/v2CreateContact
"""
return call_api(api_token, BEXIO_API_CONTACT_URL, data)
def create_contact_relation(api_token, data=None):
"""
Creates a contact relation using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`):
Contact relation data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created contact relation.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Contact-Relations/operation/v2CreateContactRelation
"""
return call_api(api_token, BEXIO_API_CONTACT_RELATION_URL, data)
def create_invoice(api_token, data=None):
"""
Creates an invoice using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`, optional):
Invoice data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created invoice.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Invoices/operation/v2CreateInvoice
"""
return call_api(api_token, BEXIO_API_INVOICE_URL, data)
def create_item(api_token, data=None):
"""
Creates an item using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`, optional):
Item data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created item.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Items/operation/v2CreateItem
"""
return call_api(api_token, BEXIO_API_ITEM_URL, data)
def create_project(api_token, data=None):
"""
Creates a project using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`, optional):
Project data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created project.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Projects/operation/v2CreateProject
"""
return call_api(api_token, BEXIO_API_PROJECT_URL, data)
def create_timesheet(api_token, data=None):
"""
Creates a timesheet using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **data** (`dict`, optional):
Timesheet data to be created, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the created timesheet.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Timesheets/operation/v2CreateTimesheet
"""
return call_api(api_token, BEXIO_API_TIMESHEET_URL, data)
def delete_contact_relation(api_token, contact_relation_id):
"""
Deletes a contact relation using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **contact_relation_id** (`int`):
ID of the contact relation to delete.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the deletion status.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Contact-Relations/operation/v2DeleteContactRelation
"""
return call_api(
api_token,
BEXIO_API_CONTACT_RELATION_URL + '/' + str(contact_relation_id),
method='DELETE',
)
def edit_contact(api_token, contact_id, data=None):
"""
Edits a contact using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **contact_id** (`int`):
ID of the contact to edit.
- **data** (`dict`):
Contact data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited contact.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Contacts/operation/v2EditContact
"""
return call_api(api_token, BEXIO_API_CONTACT_URL + '/' + str(contact_id), data)
def edit_contact_relation(api_token, contact_relation_id, data=None):
"""
Edits a contact relation using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **contact_relation_id** (`int`):
ID of the contact relation to edit.
- **data** (`dict`):
Contact relation data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited contact relation.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Contact-Relations/operation/v2EditContactRelation
"""
return call_api(
api_token,
BEXIO_API_CONTACT_RELATION_URL + '/' + str(contact_relation_id),
data,
)
def edit_invoice(api_token, invoice_id, data=None):
"""
Edits an invoice using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **invoice_id** (`int`):
ID of the invoice to edit.
- **data** (`dict`, optional):
Invoice data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited invoice.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Invoices/operation/v2EditInvoice
"""
return call_api(api_token, BEXIO_API_INVOICE_URL + '/' + str(invoice_id), data)
def edit_item(api_token, item_id, data=None):
"""
Edits an item using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **item_id** (`int`):
ID of the item to edit.
- **data** (`dict`, optional):
Item data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited item.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Items/operation/v2EditItem
"""
return call_api(api_token, BEXIO_API_ITEM_URL + '/' + str(item_id), data)
def edit_project(api_token, project_id, data=None):
"""
Edits a project using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **project_id** (`int`):
ID of the project to edit.
- **data** (`dict`, optional):
Project data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited project.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Projects/operation/v2EditProject
"""
return call_api(api_token, BEXIO_API_PROJECT_URL + '/' + str(project_id), data)
def edit_timesheet(api_token, timesheet_id, data=None):
"""
Edits a timesheet using the Bexio API.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **timesheet_id** (`int`):
ID of the timesheet to edit.
- **data** (`dict`, optional):
Project data to be edited, as per the Bexio API documentation.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`dict` | `str`):
- On success, a dictionary of the edited timesheet.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Timesheets/operation/v2EditTimesheet
"""
return call_api(api_token, BEXIO_API_TIMESHEET_URL + '/' + str(timesheet_id), data)
def fetch_accounts(api_token):
"""
Fetches all accounts from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all accounts as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Accounts/operation/v2ListAccounts
"""
return get_all(api_token, BEXIO_API_ACCOUNT_URL)
def fetch_bank_accounts(api_token):
"""
Fetches all bank accounts from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all bank accounts as dictionaries.
- On failure, an error message string.
### Notes
- Refer to API doc: https://docs.bexio.com/#tag/Bank-Accounts/operation/ListBankAccounts
"""
return get_all(api_token, BEXIO_API_BANK_ACCOUNT_URL)
def fetch_business_activities(api_token):
"""
Fetches all business activities from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all business activities as dictionaries.
- On failure, an error message string.
### Notes
- Refer to API doc:
https://docs.bexio.com/#tag/Business-Activities/operation/v2ListBusinessActivities
"""
return get_all(api_token, BEXIO_API_BUSINESS_ACTIVITY_URL)
def fetch_contact_groups(api_token):
"""
Fetches all contact groups from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all contact groups as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Contact-Groups/operation/v2ListContactGroups
"""
return get_all(api_token, BEXIO_API_CONTACT_GROUP_URL)
def fetch_contact_relations(api_token):
"""
Fetches all contact relations from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all contact relations as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Contact-Relations/operation/v2ListContactRelations
"""
return get_all(api_token, BEXIO_API_CONTACT_RELATION_URL)
def fetch_contact_sectors(api_token):
"""
Fetches all contact sectors (branches) from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all contact sectors (branches) as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Contact-Sectors/operation/v2ListContactSectors
"""
return get_all(api_token, BEXIO_API_CONTACT_SECTOR_URL)
def fetch_contacts(api_token, archived=False):
"""
Fetches all (optionally including archived) contacts from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
- **archived** (`bool`, optional):
If `True`, also request archived contacts from the API. Defaults to `False`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all contacts as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Contacts/operation/v2ListContacts
"""
return get_all(api_token, BEXIO_API_CONTACT_URL, {'show_archived': archived})
def fetch_countries(api_token):
"""
Fetches all countries from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all countries as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Countries/operation/v2ListCountries
"""
return get_all(api_token, BEXIO_API_COUNTRY_URL)
def fetch_currencies(api_token):
"""
Fetches all currencies from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all currencies as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Currencies/operation/ListCurrencies
"""
return get_all(api_token, BEXIO_API_CURRENCY_URL)
def fetch_invoices(api_token):
"""
Fetches all invoices from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all invoices as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Invoices/operation/v2ListInvoices
"""
return get_all(api_token, BEXIO_API_INVOICE_URL)
def fetch_items(api_token):
"""
Fetches all items from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all items as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Items/operation/v2ListItems
"""
return get_all(api_token, BEXIO_API_ITEM_URL)
def fetch_languages(api_token):
"""
Fetches all languages from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all languages as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Languages/operation/v2ListLanguages
"""
return get_all(api_token, BEXIO_API_LANGUAGE_URL)
def fetch_payment_types(api_token):
"""
Fetches all payment types from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all payment types as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Payment-Types/operation/v2ListPaymentTypes
"""
return get_all(api_token, BEXIO_API_PAYMENT_TYPE_URL)
def fetch_project_statuses(api_token):
"""
Fetches all project statuses from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all project statuses as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Projects/operation/v2ListProjectStatus
"""
return get_all(api_token, BEXIO_API_PROJECT_STATUS_URL)
def fetch_project_types(api_token):
"""
Fetches all project types from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all project types as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Projects/operation/v2ListProjectType
"""
return get_all(api_token, BEXIO_API_PROJECT_TYPE_URL)
def fetch_projects(api_token):
"""
Fetches all projects from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all projects as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Projects/operation/v2ListProjects
"""
return get_all(api_token, BEXIO_API_PROJECT_URL)
def fetch_salutations(api_token):
"""
Fetches all salutations from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all salutations as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Salutations/operation/v2ListSalutations
"""
return get_all(api_token, BEXIO_API_SALUTATION_URL)
def fetch_stock_areas(api_token):
"""
Fetches all stock areas from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all stock areas as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Stock-Areas/operation/v2ListStockAreas
"""
return get_all(api_token, BEXIO_API_STOCK_AREA_URL)
def fetch_stock_locations(api_token):
"""
Fetches all stock locations from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all stock locations as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc:
https://docs.bexio.com/#tag/Stock-locations/operation/v2ListStockLocations
"""
return get_all(api_token, BEXIO_API_STOCK_LOCATION_URL)
def fetch_taxes(api_token):
"""
Fetches all taxes from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all taxes as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Taxes/operation/ListTaxes
"""
return get_all(api_token, BEXIO_API_TAX_URL)
def fetch_timesheet_statuses(api_token):
"""
Fetches all timesheet statuses from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all timesheet statuses as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Timesheets/operation/v2ListTimesheets
"""
return get_all(api_token, BEXIO_API_TIMESHEET_STATUS_URL)
def fetch_timesheets(api_token):
"""
Fetches all timesheets from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all timesheets as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Timesheets/operation/v2ListTimesheets
"""
return get_all(api_token, BEXIO_API_TIMESHEET_URL)
def fetch_titles(api_token):
"""
Fetches all titles from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all titles as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Titles/operation/v2ListTitles
"""
return get_all(api_token, BEXIO_API_TITLE_URL)
def fetch_units(api_token):
"""
Fetches all units from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all units as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/Units/operation/v2ListUnits
"""
return get_all(api_token, BEXIO_API_UNIT_URL)
def fetch_users(api_token):
"""
Fetches all users from the Bexio API.
### Parameters
- **api_token** (`str`):
See `get_all()`.
### Returns
- **tuple**:
- **success** (`bool`): `True` if the request was successful, `False` otherwise.
- **result** (`list` | `str`):
- On success, a list of all users as dictionaries.
- On failure, an error message string.
### Notes
- Refer to the API doc: https://docs.bexio.com/#tag/User-Management/operation/v3ListUsers
"""
return get_all(api_token, BEXIO_API_USER_URL)
def get_all(api_token, path, params=None):
"""
A wrapper function around `call_api()` that handles the pagination of the Bexio API and
returns all items.
### Parameters
- **api_token** (`str`):
See `call_api()`.
- **path** (`str`):
See `call_api()`.
- **params** (`dict` | optional):
Additional URL parameters to be added to the request.
### Returns
- **tuple**:
See `call_api()`.
"""
if params is None:
params = {}
offset = 0
result = []
# highest limit of all endpoints. let's use that, if the max is less that's fine as well
max_limit = 2000
while True:
params['offset'] = offset
params['limit'] = max_limit
current_path = f'{path}?{urllib.parse.urlencode(params)}'
success, current_result = call_api(api_token, current_path)
if not success:
return success, current_result
result.extend(current_result)
# we get an empty list if the offset is too high
if len(current_result) == 0:
break
offset += len(current_result)
return (True, result)