43171df327acc536efb39bf34b316b136839b0ca
David Blume Add a dataclass and dataval...

David Blume authored 3 years ago

1) #!/usr/bin/env python3
2) #from https://python.plainenglish.io/taming-your-python-dictionaries-with-dataclasses-marshmallow-and-desert-388dbffedaec
3) from dataclasses import dataclass
4) import json
5) from marshmallow import EXCLUDE, fields, post_load, Schema, validate
6) import requests
7) 
8) @dataclass
9) class Activity:
10)     activity: str
11)     participants: int
12)     price: float
13) 
14) class ActivitySchema(Schema):
15)     activity = fields.Str(required=True)
16)     participants = fields.Int(
17)         required=True,
18)         validate=validate.Range(min=1, max=50, error="Participants must be between 1 and 50 people")
19)     )
20)     price = fields.Float(
21)         required=True,
22)         validate=validate.Range(min=0, max=1, error="Price must be between $1 and $100")
23)     )
24) 
25)     @post_load
26)     def transform_price(self, data, **kwargs):
27)         # The `price` key always has the price between 0 and 1 for some reason so
28)         # let's convert that number into dollars
29)         data['price'] = data['price'] * 100
30)         return data
31) 
32) 
33) def get_activity() -> Activity:
dblume boredapi.com cert expired....

dblume authored 1 year ago

34)     # Restore https when https://github.com/drewthoennes/Bored-API/issues/61 fixed
35)     resp = requests.get("http://www.boredapi.com/api/activity").json()