Skip to content

Commit

Permalink
Refactor how api endpoints are accessed
Browse files Browse the repository at this point in the history
 - Deprecated `Driver.api['']` calls.
 - Use `warnings` and `DeprecationWarning` instead of logger

 fix #5
  • Loading branch information
Vaelor committed Nov 26, 2017
1 parent e3237b9 commit aa8b1b7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 31 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
4.0.2
'''''
This release makes some internal changes on how the endpoints are accessed.

Since this works much better then using `api['endpoint']` has been deprecated for the next Major release.

Fixes https://github.com/Vaelor/python-mattermost-driver/issues/5


4.0.1
'''''
The release 4.0.0 was not quite correct, since the following changes did not really happen, only the api documentation for mattermost 4.4.0 changed.

.. code:: none
Endpoints moved from team to channels https://github.com/mattermost/mattermost-api-reference/pull/298/files
- get_public_channels
- get_deleted_channels
- search_channels
4.0.0
'''''
This has some changes related to Mattermost 4.4
Expand Down
26 changes: 13 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,28 @@ Usage
# and sets everything up in the client.
foo.login()
# You can make api calls by using api['yourendpointofchoice'].
# Since v4.0.0 you can now also call the endpoint directly.
# So, for example, wherever you see `Driver.api['users'].get_user('me')`,
# you can just do `Driver.users.get_user('me')`.
# You can make api calls by using calling `Driver.endpointofchoice`.
# Using api[''] is deprecated for 5.0.0!
# So, for example, if you used `Driver.api['users'].get_user('me')` before,
# you now just do `Driver.users.get_user('me')`.
# The names of the endpoints and requests are almost identical to
# the names on the api.mattermost.com/v4 page.
# API calls always return the json the server send as a response.
foo.api['users'].get_user_by_username('another.name')
foo.users.get_user_by_username('another.name')
# If the api request needs additional parameters
# you can pass them to the function in the following way:
# - Path parameters are always simple parameters you pass to the function
foo.api['users'].get_user(user_id='me')
foo.users.get_user(user_id='me')
# - Query parameters are always passed by passing a `params` dict to the function
foo.api['teams'].get_teams(params={...})
foo.teams.get_teams(params={...})
# - Request Bodies are always passed by passing an `options` dict or array to the function
foo.api['channels'].create_channel(options={...})
foo.channels.create_channel(options={...})
# See the mattermost api documentation to see which parameters you need to pass.
foo.api['channels'].create_channel(options={
foo.channels.create_channel(options={
'team_id': 'some_team_id',
'name': 'awesome-channel',
'display_name': 'awesome channel',
Expand All @@ -101,21 +101,21 @@ Usage
foo.init_websocket(event_handler)
# To upload a file you will need to pass a `files` dictionary
channel_id = foo.api['channels'].get_channel_by_name_and_team_name('team', 'channel')['id']
file_id = foo.api['files'].upload_file(
channel_id = foo.channels.get_channel_by_name_and_team_name('team', 'channel')['id']
file_id = foo.files.upload_file(
channel_id=channel_id
files={'files': (filename, open(filename))})['file_infos'][0]['id']
# track the file id and pass it in `create_post` options, to attach the file
foo.api['posts'].create_post(options={
foo.posts.create_post(options={
'channel_id': channel_id,
'message': 'This is the important file',
'file_ids': [file_id]})
# If needed, you can make custom requests by calling `make_request`
foo.client.make_request('post', '/endpoint', options=None, params=None, data=None, files=None, basepath=None)
# If you want to call a webhook/execute it use the `call_webhook` method.
# This method does not exist on the mattermost api AFAIK, I added it for ease of use.
foo.api['hooks'].call_webhook('myHookId', options) # Options are optional
foo.hooks.call_webhook('myHookId', options) # Options are optional
.. inclusion-marker-end-usage
Expand Down
86 changes: 79 additions & 7 deletions src/mattermostdriver/driver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import warnings

from .client import Client
from .websocket import Websocket
Expand All @@ -25,6 +26,7 @@
log = logging.getLogger('mattermostdriver.api')
log.setLevel(logging.INFO)


class Driver:
"""
Contains the client, api and provides you with functions for
Expand Down Expand Up @@ -74,7 +76,7 @@ def __init__(self, options=default_options, client_cls=Client):
self.options.update(options)
self.driver = self.options
self.client = client_cls(self.options)
self.api = {
self._api = {
'users': Users(self.client),
'teams': Teams(self.client),
'channels': Channels(self.client),
Expand All @@ -94,9 +96,6 @@ def __init__(self, options=default_options, client_cls=Client):
'elasticsearch': Elasticsearch(self.client),
'data_retention': DataRetention(self.client),
}
for k, v in self.api.items():
assert not hasattr(self, k)
setattr(self, k, v)
self.websocket = None

def init_websocket(self, event_handler, websocket_cls=Websocket):
Expand Down Expand Up @@ -132,9 +131,9 @@ def login(self):
"""
if self.options['token']:
self.client.token = self.options['token']
result = self.api['users'].get_user('me')
result = self.users.get_user('me')
else:
result = self.api['users'].login_user({
result = self.users.login_user({
'login_id': self.options['login_id'],
'password': self.options['password'],
'token': self.options['mfa_token']
Expand Down Expand Up @@ -162,4 +161,77 @@ def logout(self):
self.client.userid = ''
self.client.username = ''
self.client.cookies = None
return self.api['users'].logout_user()
return self.users.logout_user()

@property
def api(self):
warnings.warn('Deprecated for 5.0.0. Use the endpoints directly instead.', DeprecationWarning)
return self._api

@property
def users(self):
return Users(self.client)

@property
def teams(self):
return Teams(self.client)

@property
def channels(self):
return Channels(self.client)

@property
def posts(self):
return Posts(self.client)

@property
def files(self):
return Files(self.client)

@property
def preferences(self):
return Preferences(self.client)

@property
def emoji(self):
return Emoji(self.client)

@property
def system(self):
return System(self.client)

@property
def webhooks(self):
return Webhooks(self.client)

@property
def compliance(self):
return Compliance(self.client)

@property
def cluster(self):
return Cluster(self.client)

@property
def brand(self):
return Brand(self.client)

@property
def oauth(self):
return OAuth(self.client)

@property
def saml(self):
return SAML(self.client)

@property
def ldap(self):
return LDAP(self.client)

@property
def elasticsearch(self):
return Elasticsearch(self.client)

@property
def data_retention(self):
return DataRetention(self.client)
21 changes: 11 additions & 10 deletions src/mattermostdriver/endpoints/teams.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import logging
import warnings

from .base import Base
from .users import Users

log = logging.getLogger('mattermostdriver.teams')
log.setLevel(logging.INFO)


class Teams(Base):
endpoint = '/teams'
Expand Down Expand Up @@ -150,26 +148,29 @@ def get_invite_info_for_team(self, invite_id):
)

def get_public_channels(self, team_id, params=None):
log.warning(
'Using deprecated endpoint Teams.get_public_channels(). Use Channels.get_public_channels() instead.'
warnings.warn(
'Using deprecated endpoint Teams.get_public_channels(). Use Channels.get_public_channels() instead.',
DeprecationWarning
)
return self.client.get(
self.endpoint + '/' + team_id + '/channels',
params=params
)

def get_deleted_channels(self, team_id, params=None):
log.warning(
'Using deprecated endpoint Teams.get_deleted_channels(). Use Channels.get_deleted_channels() instead.'
warnings.warn(
'Using deprecated endpoint Teams.get_deleted_channels(). Use Channels.get_deleted_channels() instead.',
DeprecationWarning
)
return self.client.get(
self.endpoint + '/' + team_id + '/channels/deleted',
params=params
)

def search_channels(self, team_id, options=None):
log.warning(
'Using deprecated endpoint Teams.search_channels(). Use Channels.search_channels() instead.'
warnings.warn(
'Using deprecated endpoint Teams.search_channels(). Use Channels.search_channels() instead.',
DeprecationWarning
)
return self.client.post(
self.endpoint + '/' + team_id + '/channels/search',
Expand Down
2 changes: 1 addition & 1 deletion src/mattermostdriver/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
full_version = '4.0.1'
full_version = '4.0.2'
short_version = '.'.join(full_version.split('.', 2)[:2])
1 change: 1 addition & 0 deletions src/mattermostdriver/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
log = logging.getLogger('mattermostdriver.websocket')
log.setLevel(logging.INFO)


class Websocket:
def __init__(self, options, token):
self.options = options
Expand Down

0 comments on commit aa8b1b7

Please sign in to comment.