<commit_before>""" minimal OneDrive API

"""

import requests


class OneDrive(object):
    """ OneDrive basic API providing helpers to ease query

    """

    FILE_FACET = 'file'
    FOLDER_FACET = 'folder'
    IMAGE_FACET = 'image'
    PHOTO_FACET = 'photo'
    AUDIO_FACET = 'audio'
    VIDEO_FACET = 'video'
    LOCATION_FACET = 'location'
    DELETED_FACET = 'deleted'

    def __init__(self, token, refresh_token, client_id, client_secret,
                 refresh_callback=None):
        self.token = token
        self.refresh_token = refresh_token
        self.client_id = client_id
        self.client_secret = client_secret
        self.refresh_callback = refresh_callback

    def __token_params(self, params):
        """ add an access_token to the params dict

        @param params: request's original params
        @return: the modified dict
        """
        if params:
            params['access_token'] = self.token
        else:
            params = {'access_token': self.token}
        return params

    def __bearer_headers(self):
        """ Generate an authorization header for a request

        """
        return {'Authorization': 'Bearer {token}'.format(token=self.token)}

    def __request(self, method, path, params=None, data=None, stream=False):
        """ Run a request on OneDrive APIs

        @param method: HTTP verb
        @param path: resource endpoint
        @param params: request's parameters
        @rtype: Response
        @return: API's response
        """
        url = 'https://apis.live.net/v5.0/%s' % path
        headers = None

        if method is 'get' or 'delete':
            params = self.__token_params(params)
        else:
            headers = self.__bearer_headers()

        response = self.__do_request(
            method, url, headers, params, data, stream
        )

        if response.status_code == 401:
            self.__refresh_token()
            if method is 'get' or 'delete':
                params = self.__token_params(params)
            else:
                headers = self.__bearer_headers()
            return self.__do_request(<commit_msg>New `absolute` parameter to OneDrive.__request method<commit_after>""" minimal OneDrive API

"""

import requests


class OneDrive(object):
    """ OneDrive basic API providing helpers to ease query

    """

    FILE_FACET = 'file'
    FOLDER_FACET = 'folder'
    IMAGE_FACET = 'image'
    PHOTO_FACET = 'photo'
    AUDIO_FACET = 'audio'
    VIDEO_FACET = 'video'
    LOCATION_FACET = 'location'
    DELETED_FACET = 'deleted'

    def __init__(self, token, refresh_token, client_id, client_secret,
                 refresh_callback=None):
        self.token = token
        self.refresh_token = refresh_token
        self.client_id = client_id
        self.client_secret = client_secret
        self.refresh_callback = refresh_callback

    def __token_params(self, params):
        """ add an access_token to the params dict

        @param params: request's original params
        @return: the modified dict
        """
        if params:
            params['access_token'] = self.token
        else:
            params = {'access_token': self.token}
        return params

    def __bearer_headers(self):
        """ Generate an authorization header for a request

        """
        return {'Authorization': 'Bearer {token}'.format(token=self.token)}

    def __request(self, method, path, params=None, data=None, stream=False, absolute=False):
        """ Run a request on OneDrive APIs

        @param method: HTTP verb
        @param path: resource endpoint
        @param params: request's parameters
        @param absolute: wether the `path` parameter provides full URL or not.
        @rtype: requests.Response
        @return: API's response
        """
        if absolute:
            url = path
        else:
            url = 'https://apis.live.net/v5.0/' + path
        headers = None

        if method is 'get' or 'delete':
            params = self.__token_params(params)
        else:
            headers = self.__bearer_headers()

        response = self.__do_request(
            method, url, headers, params, data, stream
        )

        if response.status_code == 401:
            self.__refresh_token()
            if method is 'get' or 'delete':
                params = self.__token_params(params)
            else:
                headers = self.__bearer_headers()
            return self.__do_request(
{'fraction_non_alphanumeric': 0.09373601789709173, 'fraction_numerical': 0.0022371364653243847, 'mean_word_length': 2.1331464611072177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069165', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1220, 'n_words': 345}