def process_request(self, req, resp): resp.set_header('Access-Control-Allow-Origin', '*') resp.set_header('Access-Control-Allow-Methods', '*') resp.set_header('Access-Control-Allow-Headers', '*') resp.set_header('Access-Control-Max-Age', 1728000) # 20 days if req.method == 'OPTIONS': raise HTTPStatus(falcon.HTTP_200, body='\n')
def process_request(self, req: Request, res: Response) -> None: res.set_header('Access-Control-Allow-Origin', '*') res.set_header('Access-Control-Allow-Methods', '*') res.set_header('Access-Control-Allow-Headers', '*') res.set_header('Access-Control-Max-Age', 600) if req.method == 'OPTIONS': raise HTTPStatus(HTTP_200)
def write_response(req: Request, res: Response) -> None: res.content_type = 'application/cbor' res.set_header('Access-Control-Allow-Origin', '*') if req.path.endswith('/_entry-names'): path = root_dir / req.path[1:-len('/_entry-names')] if path.is_file(): raise HTTPStatus(HTTP_404) res.data = cbor2.dumps(dict( type='plain-object', content=sorted([ re.sub(r'\.h5$', '', p.name) + ('/' if p.is_dir() else '') for p in path.glob('[!_]*') ]) )) elif req.path.endswith('/_meta'): key = req.path[1:-len('/_meta')] res.data = cbor2.dumps(_read_meta(root_dir, key)) else: t_last = float(req.get_param('t_last') or 0) / 1000 entry = _read(root_dir, req.path[1:], t_last) if entry['type'] == 'file': res.data = (root_dir / entry['content']).read_bytes() else: res.data = cbor2.dumps(entry) res.status = HTTP_200
def _read_meta(root: Path, key: str) -> Dict[str, object]: path = root / key / '_meta.yaml' if path.parent.is_file(): raise HTTPStatus(HTTP_404) try: meta = yaml.safe_load(path.read_text()) except: meta = dict(spec=None, status='done') return dict(type='plain-object', content=meta)
def on_get(self, req, resp): """ Search for events. Allows filtering based on a number of parameters, detailed below. Also returns only the users who are paid to be on call. Uses response from oncall-bonus to identify paid status. **Example request**: .. sourcecode:: http GET /api/v0/oncall_events?team=foo-sre&end__gt=1487466146&role=primary HTTP/1.1 Host: example.com **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "ldap_user_id": [ { "start": 1488441600, "end": 1489132800, "team": "foo-sre", "link_id": null, "schedule_id": null, "role": "primary", "user": "******", "full_name": "Foo Icecream", "id": 187795 }, { "start": 1488441600, "end": 1489132800, "team": "foo-sre", "link_id": "8a8ae77b8c52448db60c8a701e7bffc2", "schedule_id": 123, "role": "primary", "user": "******", "full_name": "Bar Apple", "id": 187795 } ] ] :query team: team name :query user: user name :query role: role name :query id: id of the event :query start: start time (unix timestamp) of event :query end: end time (unix timestamp) of event :query start__gt: start time (unix timestamp) greater than :query start__ge: start time (unix timestamp) greater than or equal :query start__lt: start time (unix timestamp) less than :query start__le: start time (unix timestamp) less than or equal :query end__gt: end time (unix timestamp) greater than :query end__ge: end time (unix timestamp) greater than or equal :query end__lt: end time (unix timestamp) less than :query end__le: end time (unix timestamp) less than or equal :query role__eq: role name :query role__contains: role name contains param :query role__startswith: role name starts with param :query role__endswith: role name ends with param :query team__eq: team name :query team__contains: team name contains param :query team__startswith: team name starts with param :query team__endswith: team name ends with param :query team_id: team id :query user__eq: user name :query user__contains: user name contains param :query user__startswith: user name starts with param :query user__endswith: user name ends with param :statuscode 200: no error :statuscode 400: bad request """ config = self.config oncall_bonus_blacklist = config.get('bonus_blacklist', []) oncall_bonus_whitelist = config.get('bonus_whitelist', []) bonus_url = config.get('bonus_url', None) ldap_grouping = defaultdict(list) # if start time is not specified only fetch events in the future if not req.params.get('start__gt'): req.params['start__gt'] = str(int(time.time())) get_events(req, resp) # fetch team data from an externall oncall-bonus api try: bonus_response = requests.get(bonus_url) bonus_response.raise_for_status() except requests.exceptions.RequestException: raise HTTPStatus('503 failed to contact oncall-bonus API') oncall_bonus_teams = bonus_response.json() for event in json.loads(resp.body): if event['role'].lower() == 'manager': continue team = event['team'] if team in oncall_bonus_whitelist: ldap_grouping[event['user']].append(event) continue if team in oncall_bonus_blacklist: continue # check if event's role is payed for that team team_payment_details = next((item for item in oncall_bonus_teams if item.get('name', '') == team), None) if team_payment_details: team_payed_roles = { 'primary': team_payment_details.get('primary_paid', 0), 'secondary': team_payment_details.get('secondary_paid', 0) } if team_payed_roles.get(event['role']): ldap_grouping[event['user']].append(event) resp.status = HTTP_200 resp.body = json_dumps(ldap_grouping)