# Copyright (c) LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
# See LICENSE in the project root for license information.

import requests
from requests.exceptions import RequestException
import logging

logger = logging.getLogger()


class oncall(object):
    def __init__(self, config):
        headers = requests.utils.default_headers()
        headers['User-Agent'] = 'iris-api role lookup (%s)' % headers.get('User-Agent')
        self.requests = requests.session()
        self.requests.headers = headers
        self.requests.verify = False
        self.endpoint = config['oncall-api'] + '/api/v0'

    def call_oncall(self, url):
        url = str(self.endpoint + url)
        try:
            r = self.requests.get(url)
        except RequestException:
            logger.exception('Failed hitting oncall-api for url "%s"', url)
            return None

        if r.status_code != 200:
            logger.error('Invalid response from oncall-api for URL "%s". Code: %s. Content: "%s"', url, r.status_code, r.content)
            return None

        try:
            return r.json()
        except ValueError:
            logger.exception('Failed decoding json from oncall-api. URL: "%s" Code: %s', url, r.status_code)
            return None

    def team_members(self, team_name):
        result = self.call_oncall('/teams/%s/users' % team_name)
        if not isinstance(result, list):
            return None
        return result

    def team_manager(self, team_name):
        result = self.call_oncall('/teams/%s/rosters/managers' % team_name)
        if not result:
            return None
        return result['users']

    def team_oncall(self, team_name, oncall_type='primary'):
        result = self.call_oncall('/teams/%s/oncall/%s' % (team_name, oncall_type))
        if not result:
            return None
        return [user['username'] for user in result]

    def team_list(self):
        result = self.call_oncall('/teams')
        if not isinstance(result, list):
            return None
        return result

@@ -218,16 +218,38 @@
 ception%0A
+from time import time%0A
 import l
@@ -283,16 +283,24 @@
 tLogger(
+__name__
 )%0A%0A%0Aclas
@@ -1536,32 +1536,59 @@
 lf, team_name):%0A
+        now = int(time())%0A%0A
         result =
@@ -1611,37 +1611,64 @@
 l('/
-teams/%25s/rosters/manager
+events?role=manager&team=%25s&start__le=%25s&end__ge=%25
 s' %25 
+(
 team
@@ -1664,32 +1664,43 @@
 %25s' %25 (team_name
+, now, now)
 )%0A        if not
@@ -1700,74 +1700,439 @@
  if 
-not result:%0A            return None%0A        return result%5B'users'%5D
+result:%0A            return %5Buser%5B'user'%5D for user in result%5D%0A%0A        result = self.call_oncall('/teams/%25s/admins' %25 team_name)%0A        if result:%0A            logger.warning('Failed looking up manager events, defaulting to team admins list, for team %25s', team_name)%0A            return result%0A%0A        logger.error('Failed looking up manager events, as well as defaulting to team admins, for team %25s', team_name)%0A        return None
 %0A%0A  

Fix oncall api to use events endpoint to get manager roster (#7)