Skip to content

Commit 7018a34

Browse files
authored
Merge pull request #21 from jplacht/feat-cache
Integrate caching as package extra with `requests-cache`
2 parents 602d1a9 + 11909cb commit 7018a34

File tree

19 files changed

+771
-426
lines changed

19 files changed

+771
-426
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ omit =
88
# omit abstract endpoints
99
*/abstracts/*
1010
# omit config due to base_config
11-
*/config.py
11+
# */config.py

docs/caching.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Caching
2+
3+
By default, FIO Wrapper does not include any form of caching for FIO requests. Enabling caching can be extremely helpful to minimize load on FIO and reduce wait times. You can install the wrapper with the `cache` extra.
4+
5+
## Installation of FIO Wrapper with Cache Extra
6+
```python
7+
pip install fio-wrapper[cache]
8+
```
9+
10+
FIO Wrapper will now make use of [requests-cache](https://requests-cache.readthedocs.io/en/stable/index.html) as a persistent HTTP cache on top of Python's `requests` library. This package will only use `memory` caching.
11+
12+
## Cache configuration
13+
Caching can only be enabled with a [user configuration file](config.md) provided when instantiating the `FIO` wrapper.
14+
15+
### Minimal
16+
There are two minimal configuration options to be set:
17+
18+
```yaml
19+
cache:
20+
enabled: true
21+
default_expire: 3600
22+
```
23+
24+
This user configuration enables the caching of requests and sets the default expiration to 10 seconds.
25+
26+
### Advanced
27+
28+
You are able to provide URL patterns matching [integrated endpoints](routes.md) to cache and specify their individual expiration with the following options:
29+
30+
- `int` = Number of Seconds (e.g., `120` = 2 minutes)
31+
- `NEVER_EXPIRE` = route will always use cache and never expire
32+
- `DO_NOT_CACHE` = route will never be cached
33+
34+
A minimal example of caching individual urls:
35+
36+
```yaml
37+
cache:
38+
urls:
39+
"*/material/*": 3600
40+
"*/exchange/all": NEVER_EXPIRE
41+
"*/exchange/full": DO_NOT_CACHE
42+
```
43+
44+
**Note**: Make sure to adjust the configuration according to your specific caching needs.

docs/config.md

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,53 @@
22

33
FIO Wrapper internally uses a configuration file to provide its standard capabilities. The configuration can be [overwritten](usage.md) by the user.
44

5+
## `base.yml`
6+
7+
```yaml
8+
fio:
9+
application: FIO Wrapper
10+
version: 1.0.0
11+
base_url: https://rest.fnar.net
12+
timeout: 10
13+
ssl_verify: true
14+
versions:
15+
- 1.0.0
16+
cache:
17+
enabled: false
18+
default_expire: 3600
19+
fio_urls:
20+
1.0.0:
21+
material_base: /material
22+
material_all: /allmaterials
23+
exchange_base: /exchange
24+
exchange_orders: /orders
25+
exchange_all: /all
26+
exchange_full: /full
27+
building_base: /building
28+
building_all: /allbuildings
29+
recipe_base: /recipes
30+
recipe_all: /allrecipes
31+
planet_base: /planet
32+
planet_all: /allplanets
33+
planet_full: /allplanets/full
34+
planet_sites: /sites
35+
planet_search: /search
36+
localmarket_base: /localmarket
37+
localmarket_planet: /planet
38+
localmarket_shipping_source: /shipping/source
39+
localmarket_shipping_destination: /shipping/destination
40+
localmarket_company: /company
41+
sites_base: /sites
42+
sites_planets: /planets
43+
sites_warehouses: /warehouses
44+
storage_base: /storage
45+
storage_planets: /planets
46+
groups: /auth/groups
47+
groups_group: /auth/group
48+
groups_groupmemberships: /auth/groupmemberships
49+
groups_hub: /fioweb/grouphub
50+
groups_burn: /fioweb/burn/group
551

6-
7-
## `base.ini`
8-
9-
```ini
10-
[FIO]
11-
application = FIO Wrapper
12-
version = 1.0.0
13-
base_url = https://rest.fnar.net
14-
timeout = 10
15-
ssl_verify = True
16-
versions = 1.0.0
17-
18-
[URL]
19-
# material
20-
1.0.0_material_base = /material
21-
1.0.0_material_all = /allmaterials
22-
23-
# exchange
24-
1.0.0_exchange_base = /exchange
25-
1.0.0_exchange_orders = /orders
26-
1.0.0_exchange_all = /all
27-
1.0.0_exchange_full = /full
28-
29-
# building
30-
1.0.0_building_base = /building
31-
1.0.0_building_all = /allbuildings
32-
33-
# recipe
34-
1.0.0_recipe_base = /recipes
35-
1.0.0_recipe_all = /allrecipes
36-
37-
# planet
38-
1.0.0_planet_base = /planet
39-
1.0.0_planet_all = /allplanets
40-
1.0.0_planet_full = /allplanets/full
41-
1.0.0_planet_sites = /sites
42-
1.0.0_planet_search = /search
43-
44-
# localmarket
45-
1.0.0_localmarket_base = /localmarket
46-
1.0.0_localmarket_planet = /planet
47-
1.0.0_localmarket_shipping_source = /shipping/source
48-
1.0.0_localmarket_shipping_destination = /shipping/destination
49-
1.0.0_localmarket_company = /company
50-
51-
# sites
52-
1.0.0_sites_base = /sites
53-
1.0.0_sites_planets = /planets
54-
1.0.0_sites_warehouses = /warehouses
55-
56-
# storage
57-
1.0.0_storage_base = /storage
58-
1.0.0_storage_planets = /planets
59-
60-
# groups
61-
1.0.0_groups = /auth/groups
62-
1.0.0_groups_group = /auth/group
63-
1.0.0_groups_groupmemberships = /auth/groupmemberships
64-
1.0.0_groups_hub = /fioweb/grouphub
65-
1.0.0_groups_burn = /fioweb/burn/group
6652
```
6753

6854
## Config() class

docs/timeouts.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Timeouts can be defined by the user on two levels:
99

1010
FIO Wrapper includes a default fallback to `10.0` seconds, if no value is defined by the user. Overwriting to `None` to disable timeouts is possible.
1111

12-
## [`FIO`](fio.md) instantiation
12+
## [`FIO`](fio.md) Instantiation
1313

1414
Define a default timeout for all requests by overwriting the [`Adapter`](fio_adapter.md) default.
1515

@@ -23,13 +23,13 @@ fio = FIO(timeout=5)
2323

2424
FIO Wrapper will now apply the timeout of `5 seconds` to all calls it is making towards FIO.
2525

26-
## Configuration file
26+
## Configuration File
2727

28-
A standard timeout can also be defined in a users [configuration file](config.md).
28+
A standard timeout can also be defined in a user's [configuration file](config.md).
2929

30-
## Indidivual endpoint calls
30+
## Individual Endpoint Calls
3131

32-
All endpoints allow passing the optional `timeout` argument that is of type `Optional[float]` and defaults to `None`.
32+
All endpoints allow passing the optional `timeout` argument, which is of type `Optional[float]` and defaults to `None`.
3333

3434
```python
3535
from fio_wrapper import FIO
@@ -46,4 +46,4 @@ all_exchange = fio.Exchange.all(timeout=3.5)
4646

4747
```
4848

49-
It is highly recommended to not set any timeout to `None` to avoid infinite loops especially on expensive FIO endpoints like [`Group`](endpoints/group.md).
49+
It is highly recommended not to set any timeout to `None` to avoid infinite loops especially on expensive FIO endpoints like [`Group`](endpoints/group.md).

docs/usage.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,25 @@ The complete list of parameters can be found on [`FIO()`](fio.md).
4848

4949
FIO Wrapper can use a configuration file provided upon instantiation to overwrite its [base configuration](config.md).
5050

51-
Example configuration file `config.ini`:
52-
53-
```ini
54-
[FIO]
55-
application = My Awesome FIO application
56-
timeout = 3
57-
api_key = MY_FIO_API_KEY
51+
Example configuration file `config.yml`:
52+
53+
```yaml
54+
fio:
55+
application: MyExampleApplication
56+
version: 1.0.0
57+
cache:
58+
enabled: true
59+
default_expire: 10 # seconds
60+
urls:
61+
"*/material/*": 3600
62+
"*/exchange/all": NEVER_EXPIRE
63+
"*/exchange/full": DO_NOT_CACHE
5864
```
5965
6066
Use this configuration file on [`FIO()`](fio.md) by providing the `config` attribute:
6167

6268
```python
6369
from fio_wrapper import FIO
6470
65-
fio = FIO(config="config.ini")
71+
fio = FIO(config="config.yml")
6672
```

fio_wrapper/base.ini

Lines changed: 0 additions & 56 deletions
This file was deleted.

fio_wrapper/base.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
fio:
3+
application: FIO Wrapper
4+
version: 1.0.0
5+
base_url: https://rest.fnar.net
6+
timeout: 10
7+
ssl_verify: true
8+
versions:
9+
- 1.0.0
10+
cache:
11+
enabled: false
12+
default_expire: 3600
13+
fio_urls:
14+
1.0.0:
15+
material_base: /material
16+
material_all: /allmaterials
17+
exchange_base: /exchange
18+
exchange_orders: /orders
19+
exchange_all: /all
20+
exchange_full: /full
21+
building_base: /building
22+
building_all: /allbuildings
23+
recipe_base: /recipes
24+
recipe_all: /allrecipes
25+
planet_base: /planet
26+
planet_all: /allplanets
27+
planet_full: /allplanets/full
28+
planet_sites: /sites
29+
planet_search: /search
30+
localmarket_base: /localmarket
31+
localmarket_planet: /planet
32+
localmarket_shipping_source: /shipping/source
33+
localmarket_shipping_destination: /shipping/destination
34+
localmarket_company: /company
35+
sites_base: /sites
36+
sites_planets: /planets
37+
sites_warehouses: /warehouses
38+
storage_base: /storage
39+
storage_planets: /planets
40+
groups: /auth/groups
41+
groups_group: /auth/group
42+
groups_groupmemberships: /auth/groupmemberships
43+
groups_hub: /fioweb/grouphub
44+
groups_burn: /fioweb/burn/group

0 commit comments

Comments
 (0)