Пример #1
0
    def connect_impl(self, creds):
        log.debug('Connecting to box')
        if not self.__client or creds != self.__creds:
            try:
                if creds:
                    self.__creds = creds
                else:
                    raise CloudTokenError("no creds")

                jwt_token = creds.get('jwt_token')
                access_token = creds.get('access_token')
                refresh_token = creds.get('refresh_token')

                if not jwt_token:
                    if not ((self._oauth_config.app_id and self._oauth_config.app_secret) and (refresh_token or access_token)):
                        raise CloudTokenError("require app_id/secret and either access_token or refresh token")

                with self._mutex:
                    box_session = Session()
                    box_kwargs = box_session.get_constructor_kwargs()
                    box_kwargs["api_config"] = boxsdk.config.API
                    box_kwargs["default_network_request_kwargs"] = {"timeout": 60}

                    if jwt_token:
                        jwt_dict = json.loads(jwt_token)
                        user_id = creds.get('user_id')
                        auth = JWTAuth.from_settings_dictionary(jwt_dict, user=user_id,
                                                                store_tokens=self._store_refresh_token)
                    else:
                        if not refresh_token:
                            raise CloudTokenError("Missing refresh token")
                        auth = OAuth2(client_id=self._oauth_config.app_id,
                                      client_secret=self._oauth_config.app_secret,
                                      access_token=access_token,
                                      refresh_token=refresh_token,
                                      store_tokens=self._store_refresh_token)

                    box_session = AuthorizedSession(auth, **box_kwargs)
                    self.__client = Client(auth, box_session)
                with self._api():
                    self.__access_token = auth.access_token
                    self._long_poll_manager.start()
            except BoxNetworkException as e:
                log.exception("Error during connect %s", e)
                self.disconnect()
                raise CloudDisconnectedError()
            except (CloudTokenError, CloudDisconnectedError):
                raise
            except Exception as e:
                log.exception("Error during connect %s", e)
                self.disconnect()
                raise CloudTokenError()

        with self._api() as client:
            return client.user(user_id='me').get().id
Пример #2
0
 def __init__(self):
     #p=Proxy()
     #p.URL='https://bcpxy.nycnet:8080'
     #p.AUTH={'user': '******', 'password':'******'}
     #self.session=Session(proxy_config=p)
     self.session=Session()
     self.sdk = JWTAuth.from_settings_file('/code/mocj_porter/config/box_config.json')
     self.client=Client(self.sdk,self.session)
Пример #3
0
def mock_box_session_2():
    mock_session = MagicMock(Session)
    # pylint:disable=protected-access
    mock_session._api_config = API()
    mock_session._client_config = Client()
    # pylint:enable=protected-access
    mock_session.get_url.side_effect = lambda *args, **kwargs: Session.get_url(
        mock_session, *args, **kwargs)
    return mock_session
Пример #4
0
def mock_box_session(translator):
    mock_session = MagicMock(Session)
    # pylint:disable=protected-access
    mock_session._api_config = mock_session.api_config = API()
    mock_session._client_config = mock_session.client_config = Client()
    mock_session._proxy_config = mock_session.proxy_config = Proxy()
    # pylint:enable=protected-access
    mock_session.get_url.side_effect = lambda *args, **kwargs: Session.get_url(mock_session, *args, **kwargs)
    mock_session.translator = translator
    return mock_session
Пример #5
0
def unauthorized_session(mock_network_layer, translator):
    # pylint:disable=redefined-outer-name
    return Session(network_layer=mock_network_layer, translator=translator)
Пример #6
0
def unauthorized_session(network_layer):
    return Session(network_layer=network_layer)
Пример #7
0
def unauthorized_session(mock_box_network):
    return Session(network_layer=mock_box_network)