Skip to content
This repository has been archived by the owner on Oct 24, 2020. It is now read-only.

Added capability to pass client session in API #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 50 additions & 10 deletions aioslacker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
self,
token=None,
timeout=slacker.DEFAULT_TIMEOUT,
session=None,
*,
loop=None
):
Expand All @@ -29,12 +30,13 @@ def __init__(

self.loop = loop

self.session = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(
use_dns_cache=False,
loop=self.loop,
),
)
self._close_session = False

if session is None:
session = aiohttp.ClientSession(loop=self.loop)
self._close_session = True

self._session = session

self.methods = {
requests.get: 'GET',
Expand All @@ -57,6 +59,12 @@ def __request(self, method, api, **kwargs):

kwargs['params'] = urlencode(kwargs['params'], doseq=True)

kwargs['data'] = {
key: value
for key, value in kwargs.get('data', {}).items()
if value is not None
}

if method == 'POST':
files = kwargs.pop('files', None)

Expand All @@ -76,7 +84,7 @@ def __request(self, method, api, **kwargs):

_url = slacker.API_BASE_URL.format(api=api)

_request = self.session.request(method, _url, **kwargs)
_request = self._session.request(method, _url, **kwargs)

_response = None

Expand Down Expand Up @@ -118,7 +126,8 @@ def close(self):
return_exceptions=True
)

yield from self.session.close()
if self._close_session:
yield from self._session.close()


class IM(BaseAPI, slacker.IM):
Expand Down Expand Up @@ -154,6 +163,7 @@ class TeamProfile(BaseAPI, slacker.TeamProfile):


class Team(BaseAPI, slacker.Team):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._profile = TeamProfile(*args, **kwargs)
Expand Down Expand Up @@ -286,18 +296,24 @@ def __init__(
self,
url=None,
timeout=slacker.DEFAULT_TIMEOUT,
session=None,
*, loop=None
):
self.url = url

super().__init__(token=None, timeout=timeout, loop=loop)
super().__init__(
token=None,
timeout=timeout,
session=session,
loop=loop,
)

@asyncio.coroutine
def post(self, data):
if not self.url:
raise slacker.Error('URL for incoming webhook is undefined')

_request = self.session.request(
_request = self._session.request(
'POST',
self.url,
data=data,
Expand Down Expand Up @@ -327,6 +343,7 @@ def __init__(
token,
incoming_webhook_url=None,
timeout=slacker.DEFAULT_TIMEOUT,
session=None,
*, loop=None
):
if loop is None:
Expand All @@ -337,116 +354,139 @@ def __init__(
self.im = IM(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.api = API(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.dnd = DND(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.rtm = RTM(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.auth = Auth(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.bots = Bots(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.chat = Chat(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.team = Team(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.pins = Pins(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.mpim = MPIM(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.users = Users(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.files = Files(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.stars = Stars(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.emoji = Emoji(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.search = Search(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.groups = Groups(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.channels = Channels(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.presence = Presence(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.reminders = Reminders(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.reactions = Reactions(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.idpgroups = IDPGroups(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.usergroups = UserGroups(
token=token,
timeout=timeout,
session=session,
loop=self.loop,
)
self.incomingwebhook = IncomingWebhook(
url=incoming_webhook_url,
timeout=timeout,
session=session,
loop=self.loop,
)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def read(*parts):
install_requires=[
'aiohttp>=1.3.0',
'async_timeout>=2.0.0',
'slacker<=0.9.42',
'slacker',
],
packages=['aioslacker'],
include_package_data=True,
Expand Down