import requests

from allauth.socialaccount.providers.oauth2.views import (
    OAuth2Adapter,
    OAuth2CallbackView,
    OAuth2LoginView,
)

from .provider import DropboxOAuth2Provider


class DropboxOAuth2Adapter(OAuth2Adapter):
    provider_id = DropboxOAuth2Provider.id
    access_token_url = "https://api.dropbox.com/oauth2/token"
    authorize_url = "https://www.dropbox.com/oauth2/authorize"
    profile_url = "https://api.dropbox.com/2/users/get_current_account"
    redirect_uri_protocol = "https"

    def complete_login(self, request, app, token, **kwargs):
        response = requests.post(
            self.profile_url,
            headers={"Authorization": "Bearer %s" % (token.token,)},
        )
        response.raise_for_status()
        return self.get_provider().sociallogin_from_response(request, response.json())


oauth_login = OAuth2LoginView.adapter_view(DropboxOAuth2Adapter)
oauth_callback = OAuth2CallbackView.adapter_view(DropboxOAuth2Adapter)

[((28, 14, 28, 64), 'allauth.socialaccount.providers.oauth2.views.OAuth2LoginView.adapter_view', 'OAuth2LoginView.adapter_view', ({(28, 43, 28, 63): 'DropboxOAuth2Adapter'}, {}), '(DropboxOAuth2Adapter)', False, 'from allauth.socialaccount.providers.oauth2.views import OAuth2Adapter, OAuth2CallbackView, OAuth2LoginView\n'), ((29, 17, 29, 70), 'allauth.socialaccount.providers.oauth2.views.OAuth2CallbackView.adapter_view', 'OAuth2CallbackView.adapter_view', ({(29, 49, 29, 69): 'DropboxOAuth2Adapter'}, {}), '(DropboxOAuth2Adapter)', False, 'from allauth.socialaccount.providers.oauth2.views import OAuth2Adapter, OAuth2CallbackView, OAuth2LoginView\n'), ((20, 19, 23, 9), 'requests.post', 'requests.post', (), '', False, 'import requests\n')]