Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions linebot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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'}
Expand All @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_error_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions tests/api/test_rich_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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(
Expand All @@ -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
)
Expand All @@ -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
)
Expand Down