-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi2.py
45 lines (37 loc) · 1.18 KB
/
api2.py
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
# -*- coding: UTF-8 -*-
from .wrapper import Entity
from .fields import *
class Api2Entity(Entity):
default_api_base = 'http://openmensa.org/api/v2/'
def __repr__(self):
return self._type + '(' + ','.join(map(lambda v: v + '=' + repr(getattr(self, v)), self._fields)) + ')'
class Canteen(Api2Entity):
id = IntegerField()
name = StringField()
address = StringField()
latitude = FloatField()
longitude = FloatField()
def __init__(self, id=None, values={}):
super(Canteen, self).__init__()
if id:
self.fromJsonDict(self.request('canteens/{id}'.format(id=int(id))))
else:
self.fromJsonDict(values)
@staticmethod
def find(limit=None, ids=None, near=None):
recvCanteens = lambda **kwargs: list(map(lambda c: Canteen(values=c),
Canteen().request('canteens', params=kwargs)))
params = {}
if limit:
params['limit'] = limit
if ids:
params['ids'] = ','.join(map(lambda i: str(i), ids))
if near is not None:
params.update({ 'near[lat]': near[0], 'near[lng]': near[1] })
if len(near) > 2:
params['near[dist]'] = near[2]
return recvCanteens(**params)
def __str__(self):
return 'Canteen({id}: {name})'.format(**self.__dict__)
class Meal(Api2Entity):
pass