def refresh_authorization_token(self, refresh_token):
        """
        Exchange a refresh token for a new access token.

        Example response:

        {
            'access_token': 'asdf',
            'expires_in': 1209600,
            'scope': 'profile',
            'token_type': 'bearer'}
        }
        """
        params = {
            'grant_type': 'refresh_token',
            'client_id': settings.FXA_CLIENT_ID,
            'client_secret': settings.FXA_SECRET,
            'scope': settings.FXA_SCOPE,
            'refresh_token': refresh_token,
        }

        with stats_client.timer('fxa_refresh_auth_token_timing'):
            token_url = urlparse.urljoin(settings.FXA_OAUTH_URI, 'v1/token')
            response = requests.post(token_url, data=json.dumps(params))
            stats_client.incr('fxa_refresh_auth_token_count')

        return self._parse_response(response)
    def compute_ranks(cls):
        """
        Compute the number of observations and ranks for
        each contributor for each country and globally.
        """
        with stats_client.timer('compute_ranks_timing'):
            # Pull all contributions into memory,  we will only work on this
            # dataset while new contributions enter the database.
            contributions = list(Contribution.objects.all())
            cls._compute_ranks(contributions)

            stats_client.gauge('compute_ranks_contributions',
                               len(contributions))
    def compute_ranks(cls):
        """
        Compute the number of observations and ranks for
        each contributor for each country and globally.
        """
        with stats_client.timer('compute_ranks_timing'):
            # Pull all contributions into memory,  we will only work on this
            # dataset while new contributions enter the database.
            contributions = list(Contribution.objects.all())
            cls._compute_ranks(contributions)

            stats_client.gauge(
                'compute_ranks_contributions', len(contributions))
    def get_profile_data(self, access_token):
        """
        Retrieve the profile details for a user given a valid access_token.

        Example response:

        {
            'email': '*****@*****.**',
            'uid': '92e70a0155544210a787f98a4a22f6e2'
        }
        """
        headers = {
            'Authorization': 'Bearer {}'.format(access_token),
        }

        with stats_client.timer('fxa_get_profile_timing'):
            profile_url = urlparse.urljoin(
                settings.FXA_PROFILE_URI, 'v1/profile')
            response = requests.get(profile_url, headers=headers)
            stats_client.incr('fxa_get_profile_count')

        return self._parse_response(response)
    def verify_token(self, access_token):
        """
        Verify an access token with FXA.

        Example response:

        {
            "user": "******",
            "client_id": "45defeda038a1c92",
            "scope": ["profile:email", "profile:avatar"],
            "email": "*****@*****.**"
        }
        """
        with stats_client.timer('fxa_verify_auth_token_timing'):
            profile_url = urlparse.urljoin(settings.FXA_OAUTH_URI, 'v1/verify')
            response = requests.post(
                profile_url,
                {'token': access_token},
            )
            stats_client.incr('fxa_verify_auth_token_count')

        return self._parse_response(response)