Navigation Menu

Skip to content

Commit

Permalink
added lib user agent with version
Browse files Browse the repository at this point in the history
  • Loading branch information
LidorViber committed Jan 15, 2017
1 parent a3ba0cc commit f57b9ee
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions setup.py
@@ -1,8 +1,8 @@
from setuptools import setup

exec(open('viberbot/version.py').read())
setup(
name='viberbot',
version='1.0.0',
version=__version__,
packages=['viberbot', 'viberbot.api', 'viberbot.api.viber_requests',
'viberbot.api.messages', 'viberbot.api.messages.data_types'],
install_requires=['future', 'requests'],
Expand Down
3 changes: 3 additions & 0 deletions viberbot/__init__.py
@@ -1,4 +1,7 @@
from .api.api import Api
from .api.bot_configuration import BotConfiguration
from .version import __version__

__all__ = ['Api', 'BotConfiguration']
__version__ = __version__

4 changes: 2 additions & 2 deletions viberbot/api/api.py
Expand Up @@ -3,7 +3,7 @@
import json
import logging

from viberbot.api.consts import VIBER_BOT_API_URL
from viberbot.api.consts import VIBER_BOT_API_URL, VIBER_BOT_USER_AGENT
from viberbot.api.viber_requests import create_request
from viberbot.api.api_request_sender import ApiRequestSender
from viberbot.api.message_sender import MessageSender
Expand All @@ -13,7 +13,7 @@ class Api(object):
def __init__(self, bot_configuration):
self._logger = logging.getLogger('viber.bot.api')
self._bot_configuration = bot_configuration
self._request_sender = ApiRequestSender(self._logger, VIBER_BOT_API_URL, bot_configuration)
self._request_sender = ApiRequestSender(self._logger, VIBER_BOT_API_URL, bot_configuration, VIBER_BOT_USER_AGENT)
self._message_sender = MessageSender(self._logger, self._request_sender, bot_configuration)

@property
Expand Down
9 changes: 7 additions & 2 deletions viberbot/api/api_request_sender.py
Expand Up @@ -6,10 +6,11 @@


class ApiRequestSender(object):
def __init__(self, logger, viber_bot_api_url, bot_configuration):
def __init__(self, logger, viber_bot_api_url, bot_configuration, viber_bot_user_agent):
self._logger = logger
self._viber_bot_api_url = viber_bot_api_url
self._bot_configuration = bot_configuration
self._user_agent = viber_bot_user_agent

def set_webhook(self, url, webhook_events=None):
payload = {
Expand Down Expand Up @@ -41,7 +42,11 @@ def get_account_info(self):

def post_request(self, endpoint, payload):
try:
response = requests.post(self._viber_bot_api_url + '/' + endpoint, data=payload)
headers = requests.utils.default_headers()
headers.update({
'User-Agent': self._user_agent
})
response = requests.post(self._viber_bot_api_url + '/' + endpoint, data=payload, headers=headers)
response.raise_for_status()
return json.loads(response.text)
except RequestException as e:
Expand Down
4 changes: 4 additions & 0 deletions viberbot/api/consts.py
@@ -1,5 +1,7 @@
from ..version import __version__

VIBER_BOT_API_URL = "https://chatapi.viber.com/pa"
VIBER_BOT_USER_AGENT = "ViberBot-Python/" + __version__


class BOT_API_ENDPOINT(object):
Expand All @@ -8,3 +10,5 @@ class BOT_API_ENDPOINT(object):
SEND_MESSAGE = 'send_message'
GET_ONLINE = 'get_online'
GET_USER_DETAILS = 'get_user_details'


1 change: 1 addition & 0 deletions viberbot/version.py
@@ -0,0 +1 @@
__version__ = '1.0.5'

0 comments on commit f57b9ee

Please sign in to comment.