diff --git a/linebot/api.py b/linebot/api.py index 689dd88e..111d8448 100644 --- a/linebot/api.py +++ b/linebot/api.py @@ -35,13 +35,16 @@ class LineBotApi(object): """LineBotApi provides interface for LINE messaging API.""" DEFAULT_API_ENDPOINT = 'https://api.line.me' + DEFAULT_API_DATA_ENDPOINT = 'https://api-data.line.me' - def __init__(self, channel_access_token, endpoint=DEFAULT_API_ENDPOINT, + def __init__(self, channel_access_token, + endpoint=DEFAULT_API_ENDPOINT, data_endpoint=DEFAULT_API_DATA_ENDPOINT, timeout=HttpClient.DEFAULT_TIMEOUT, http_client=RequestsHttpClient): """__init__ method. :param str channel_access_token: Your channel access token :param str endpoint: (optional) Default is https://api.line.me + :param str data_endpoint: (optional) Default is https://api-data.line.me :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) float tuple. @@ -51,6 +54,7 @@ def __init__(self, channel_access_token, endpoint=DEFAULT_API_ENDPOINT, :py:class:`linebot.http_client.RequestsHttpClient` :type http_client: T <= :py:class:`linebot.http_client.HttpClient` """ + self.data_endpoint = data_endpoint self.endpoint = endpoint self.headers = { 'Authorization': 'Bearer ' + channel_access_token, @@ -441,7 +445,7 @@ def get_message_content(self, message_id, timeout=None): """ response = self._get( '/v2/bot/message/{message_id}/content'.format(message_id=message_id), - stream=True, timeout=timeout + endpoint=self.data_endpoint, stream=True, timeout=timeout ) return Content(response) @@ -664,7 +668,7 @@ def get_rich_menu_image(self, rich_menu_id, timeout=None): """ response = self._get( '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), - timeout=timeout + endpoint=self.data_endpoint, timeout=timeout ) return Content(response) @@ -687,6 +691,7 @@ def set_rich_menu_image(self, rich_menu_id, content_type, content, timeout=None) """ self._post( '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), + endpoint=self.data_endpoint, data=content, headers={'Content-Type': content_type}, timeout=timeout @@ -961,8 +966,8 @@ def get_insight_message_event(self, request_id, timeout=None): return InsightMessageEventResponse.new_from_json_dict(response.json) - def _get(self, path, params=None, headers=None, stream=False, timeout=None): - url = self.endpoint + path + def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None): + url = (endpoint or self.endpoint) + path if headers is None: headers = {} @@ -975,8 +980,8 @@ def _get(self, path, params=None, headers=None, stream=False, timeout=None): self.__check_error(response) return response - def _post(self, path, data=None, headers=None, timeout=None): - url = self.endpoint + path + def _post(self, path, endpoint=None, data=None, headers=None, timeout=None): + url = (endpoint or self.endpoint) + path if headers is None: headers = {'Content-Type': 'application/json'} @@ -989,8 +994,8 @@ def _post(self, path, data=None, headers=None, timeout=None): self.__check_error(response) return response - def _delete(self, path, data=None, headers=None, timeout=None): - url = self.endpoint + path + def _delete(self, path, endpoint=None, data=None, headers=None, timeout=None): + url = (endpoint or self.endpoint) + path if headers is None: headers = {} diff --git a/tests/api/test_error_handle.py b/tests/api/test_error_handle.py index 99e74cae..dba3fe76 100644 --- a/tests/api/test_error_handle.py +++ b/tests/api/test_error_handle.py @@ -104,7 +104,7 @@ def test_error_with_detail_handle(self): def test_error_handle_get_message_content(self): responses.add( responses.GET, - LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/message/1/content', + LineBotApi.DEFAULT_API_DATA_ENDPOINT + '/v2/bot/message/1/content', json={ "message": "Invalid reply token" }, diff --git a/tests/api/test_rich_menu.py b/tests/api/test_rich_menu.py index b770c635..44c41028 100644 --- a/tests/api/test_rich_menu.py +++ b/tests/api/test_rich_menu.py @@ -394,7 +394,7 @@ def test_get_rich_menu_image(self): body = b'hogieoidksk' responses.add( responses.GET, - LineBotApi.DEFAULT_API_ENDPOINT + + LineBotApi.DEFAULT_API_DATA_ENDPOINT + '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), body=body, status=200 ) @@ -405,7 +405,7 @@ def test_get_rich_menu_image(self): self.assertEqual(request.method, 'GET') self.assertEqual( request.url, - LineBotApi.DEFAULT_API_ENDPOINT + + LineBotApi.DEFAULT_API_DATA_ENDPOINT + '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), ) self.assertEqual( @@ -419,7 +419,7 @@ def test_set_rich_menu_image(self): body = b'hogieoidksk' responses.add( responses.POST, - LineBotApi.DEFAULT_API_ENDPOINT + + LineBotApi.DEFAULT_API_DATA_ENDPOINT + '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), json={}, status=200 ) @@ -433,7 +433,7 @@ def test_set_rich_menu_image(self): request = responses.calls[0].request self.assertEqual('POST', request.method) self.assertEqual( - LineBotApi.DEFAULT_API_ENDPOINT + + LineBotApi.DEFAULT_API_DATA_ENDPOINT + '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id), request.url )