diff --git a/Makefile b/Makefile index ffe71ec0..ad34a0aa 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,6 @@ test: lint install: #Install slackclient install -d $${DESTDIR:-/}/usr/share/localslackirc/slackclient/ - install -m644 slackclient/slackrequest.py $${DESTDIR:-/}/usr/share/localslackirc/slackclient/ install -m644 slackclient/exceptions.py $${DESTDIR:-/}/usr/share/localslackirc/slackclient/ install -m644 slackclient/client.py $${DESTDIR:-/}/usr/share/localslackirc/slackclient/ install -m644 slackclient/__init__.py $${DESTDIR:-/}/usr/share/localslackirc/slackclient/ @@ -37,7 +36,6 @@ dist: localslackirc/slackclient/__init__.py \ localslackirc/slackclient/client.py \ localslackirc/slackclient/exceptions.py \ - localslackirc/slackclient/slackrequest.py \ localslackirc/Makefile \ localslackirc/CHANGELOG \ localslackirc/LICENSE \ diff --git a/slackclient/client.py b/slackclient/client.py index abf121d4..70ed1e5d 100644 --- a/slackclient/client.py +++ b/slackclient/client.py @@ -22,18 +22,54 @@ # to the changes made since it was copied. from .exceptions import * -from .slackrequest import SlackRequest import json from typing import Any, Dict, List, NamedTuple, Optional from requests.packages.urllib3.util.url import parse_url +import requests from ssl import SSLWantReadError from typedload import load from websocket import create_connection, WebSocket from websocket._exceptions import WebSocketConnectionClosedException +class SlackRequest: + def __init__(self, proxies: Optional[Dict[str,str]] = None) -> None: + self.proxies = proxies + + def do(self, token: str, request: str, post_data: Dict[str,str], timeout: Optional[float], files: Optional[Dict]): + """ + Perform a POST request to the Slack Web API + + Args: + token (str): your authentication token + request (str): the method to call from the Slack API. For example: 'channels.list' + timeout (float): stop waiting for a response after a given number of seconds + post_data (dict): key/value arguments to pass for the request. For example: + {'channel': 'CABC12345'} + """ + domain = "slack.com" + + url = f'https://{domain}/api/{request}' + + # Set user-agent and auth headers + headers = { + 'user-agent': 'localslackirc', + 'Authorization': f'Bearer {token}' + } + + # Submit the request + return requests.post( + url, + headers=headers, + data=post_data, + timeout=timeout, + files=files, + proxies=self.proxies + ) + + class Team(NamedTuple): id: str name: str diff --git a/slackclient/exceptions.py b/slackclient/exceptions.py index 2e997c0f..af4841fd 100644 --- a/slackclient/exceptions.py +++ b/slackclient/exceptions.py @@ -25,38 +25,17 @@ class SlackClientError(Exception): """ Base exception for all errors raised by the SlackClient library """ - def __init__(self, msg=None): - if msg is None: - # default error message - msg = "An error occurred in the SlackClient library" + def __init__(self, msg: str) -> None: super(SlackClientError, self).__init__(msg) -class ParseResponseError(SlackClientError, ValueError): - """ - Error raised when responses to Web API methods cannot be parsed as valid JSON - """ - def __init__(self, response_body, original_exception): - super(ParseResponseError, self).__init__( - "Slack API response body could not be parsed: {0}. Original exception: {1}".format( - response_body, original_exception - ) - ) - self.response_body = response_body - self.original_exception = original_exception - - class SlackConnectionError(SlackClientError): - def __init__(self, message='', reply=None): + def __init__(self, message='', reply=None) -> None: super(SlackConnectionError, self).__init__(message) self.reply = reply class SlackLoginError(SlackClientError): - def __init__(self, message='', reply=None): + def __init__(self, message='', reply=None) -> None: super(SlackLoginError, self).__init__(message) self.reply = reply - - -class SlackNotConnected(Exception): - pass diff --git a/slackclient/slackrequest.py b/slackclient/slackrequest.py deleted file mode 100644 index eff684ef..00000000 --- a/slackclient/slackrequest.py +++ /dev/null @@ -1,63 +0,0 @@ -# localslackirc -# Copyright (C) 2018 Salvo "LtWorf" Tomaselli -# -# localslackirc is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -# author Salvo "LtWorf" Tomaselli -# -# This file was part of python-slackclient -# (https://github.com/slackapi/python-slackclient) -# But has been copied and relicensed under GPL. The copyright applies only -# to the changes made since it was copied. - -import json -from typing import Dict, Optional - -import requests - - -class SlackRequest: - def __init__(self, proxies=None): - self.proxies = proxies - - def do(self, token: str, request: str, post_data: Dict[str,str], timeout: Optional[float], files: Optional[Dict]): - """ - Perform a POST request to the Slack Web API - - Args: - token (str): your authentication token - request (str): the method to call from the Slack API. For example: 'channels.list' - timeout (float): stop waiting for a response after a given number of seconds - post_data (dict): key/value arguments to pass for the request. For example: - {'channel': 'CABC12345'} - """ - domain = "slack.com" - - url = f'https://{domain}/api/{request}' - - # Set user-agent and auth headers - headers = { - 'user-agent': 'localslackirc', - 'Authorization': f'Bearer {token}' - } - - # Submit the request - return requests.post( - url, - headers=headers, - data=post_data, - timeout=timeout, - files=files, - proxies=self.proxies - )