# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import requests
from furl import furl

from teambition.api.base import TeambitionAPI


class OAuth(TeambitionAPI):

    def get_authorize_url(self, redirect_uri, state='', lang='zh'):
        """
        获取授权地址

        :param redirect_uri: 授权回调地址
        :param state: 原样返回给客户端
        :param lang: 语言类型，可选 zh, en，默认为 zh
        :return: 授权地址
        """
        oauth2_url = furl('https://account.teambition.com/oauth2/authorize')
        oauth2_url.add(args={
            'client_id': self.client_id,
            'redirect_uri': redirect_uri,
            'state': state,
            'lang': lang
        })
        return oauth2_url.url

    def fetch_access_token(self, code, grant_type='code'):
        """
        根据 code 获取 access_token

        :param code: 授权完成返回的 code 参数值
        :param grant_type: 固定值为 code
        :return: access_token 值
        """
        res = self._post(
            'oauth2/access_token',
            data={
                'client_id': self.client_id,
                'client_secret': self.client_secret,
                'code': code,
                'grant_type': grant_type
            }
        )
        token = res['access_token']
        self._client._access_token = token
        return token

@@ -1307,12 +1307,519 @@
 eturn token%0A
+%0A    def check(self, access_token=None):%0A        %22%22%22%0A        %E9%AA%8C%E8%AF%81 access_token %E6%98%AF%E5%90%A6%E5%90%88%E6%B3%95%0A%0A        :param access_token: %E5%8F%AF%E9%80%89%EF%BC%8Caccess_token%0A        :return: %E8%BF%94%E5%9B%9E%E7%9A%84 JSON %E6%95%B0%E6%8D%AE%E5%8C%85%0A        %22%22%22%0A        headers = %7B%0A            'access_token': access_token or self.access_token,%0A        %7D%0A        try:%0A            res = self._get(%0A                'api/applications/%7B0%7D/tokens/check'.format(self.client_id),%0A                headers=headers%0A            )%0A        except requests.HTTPError:%0A            return False%0A        return True%0A

Add check method for OAuth