def test_client_service_builder(mock_discovery): c = Client(mock.Mock(), token='') srv = c.some_fake_service() assert c._services['some_fake_service:None'] == srv assert mock_discovery.build.called srv2 = c.some_fake_service() assert srv2 == srv assert mock_discovery.build.call_count == 1 srv3 = c.some_fake_service('v2') assert c._services['some_fake_service:v2'] == srv3 assert srv3 != srv2 assert mock_discovery.build.call_count == 2
async def getCreds(self, event=None): credentials = None if os.path.exists(G_DRIVE_TOKEN_FILE): with open(G_DRIVE_TOKEN_FILE, 'rb') as f: credentials = pickle.load(f) if credentials is None or not credentials.valid: if credentials and credentials.expired and credentials.refresh_token: credentials.refresh(Request()) with open(G_DRIVE_TOKEN_FILE, "rb") as token: saveAccessTokenDB(token.read()) with open(G_DRIVE_TOKEN_FILE, "wb") as token: pickle.dump(credentials, token) else: flow = OAuth2WebServerFlow(Config.G_DRIVE_CLIENT_ID, Config.G_DRIVE_CLIENT_SECRET, self.SCOPES, redirect_uri=self.__REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() code = "" print(authorize_url) if event: async with event.client.conversation( Config.PRIVATE_GROUP_BOT_API_ID) as conv: await conv.send_message( f"Go to the following link in your browser: {authorize_url} and reply the code" ) response = conv.wait_event( events.NewMessage( outgoing=True, chats=Config.PRIVATE_GROUP_BOT_API_ID)) response = await response code = response.message.message.strip() else: code = input("Enter CODE: ") creds = flow.step2_exchange(code) credentials = Client._make_credentials( token=creds.access_token, refresh_token=creds.refresh_token, id_token=creds.id_token, token_uri=creds.token_uri, client_id=creds.client_id, client_secret=creds.client_secret) with open(G_DRIVE_TOKEN_FILE, 'wb') as token: pickle.dump(credentials, token) with open(G_DRIVE_TOKEN_FILE, "rb") as token: saveAccessTokenDB(token.read()) return credentials
async def authorize(self, event=None): creds = await self.getCreds(event) self.service = Client(session=self.session, credentials=creds).drive("v3")
def test_client_creates_credentials_if_none_are_passed_in(): with mock.patch('gaggle.client.Client._make_credentials') as mock_maker: Client(mock.Mock()) assert mock_maker.called