-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.py
58 lines (48 loc) · 1.59 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import subprocess
import sublime
try:
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError:
from urllib import urlencode, urlopen
BASE_URL = 'https://slack.com/api/'
def api_call(method, call_args={}, loading=None, filename=None, icon=None):
if icon:
call_args['icon_url'] = icon
print('icon', icon)
URL = BASE_URL + method + "?" + urlencode(call_args)
print('calling:', URL)
try:
if filename:
f = open(filename, 'rb')
filebody = f.read()
f.close()
data = urlencode({'content': filebody})
response = urlopen(
url=URL,
data=data.encode('utf8')
).read().decode('utf8')
else:
response = urlopen(url=URL).read().decode('utf8')
except:
# fallback for sublime bug with urlopen (on linux only)
if filename: # upload filename
proc = subprocess.Popen(
['curl', '-X', 'POST', '-F', 'file=@'+filename, URL],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
else:
proc = subprocess.Popen(
['curl', '-s', URL],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate()
response = out.decode('utf8')
response = json.loads(response)
if not response['ok']:
sublime.error_message("SLACK Api error: " + response['error'])
if loading:
loading.done = True
return False
return response