-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackpad.py
More file actions
150 lines (123 loc) · 4.75 KB
/
Copy pathhackpad.py
File metadata and controls
150 lines (123 loc) · 4.75 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Simple wrapper for the Hackpad API v1.0
API Documentation: https://hackpad.com/Public-Hackpad-API-Draft-nGhsrCJFlP7
"""
import requests
import time
from requests_oauthlib import OAuth1Session
from urllib.parse import urljoin
class Hackpad(object):
def __init__(self, api_scheme='https', api_domain='hackpad.com', sub_domain='', consumer_key='', consumer_secret=''):
self.api_scheme = api_scheme
self.api_domain = api_domain
self.sub_domain = sub_domain
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
return
def create_blank_hackpad(self, asUser='', content_type='text/plain'):
return self.create_hackpad('Hackpad Title', 'Auto-generated Hackpad contents.',
asUser, content_type)
def create_hackpad(self, title, content, asUser='', content_type='text/plain'):
api_link = 'pad/create'
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'POST', params, '%s\n%s' % (title, content), content_type)
def get_pad_content(self, padId, revision='latest', response_format='txt', asUser=''):
api_link = 'pad/%s/content/%s.%s' % (padId, revision, response_format)
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def update_pad_content(self, padId, content='', asUser='', content_type='text/plain'):
api_link = 'pad/%s/content' % padId
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'POST', params, content, content_type)
def search_for_pads(self, q='', start=0, limit=10, asUser=''):
api_link = 'search'
params = {'q':q, 'start':start, 'limit':limit}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def list_updated_pads(self, timestamp):
api_link = 'edited-since%s' % timestamp
params = {}
return self.do_api_request(api_link, 'GET', params)
def pad_revisions(self, padId, asUser=''):
api_link = 'pad/%s/revisions' % padId
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def revert_pad(self, padId, revision, asUser=''):
api_link = 'pad/%s/revert-to/%s' % (padId, revision)
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'POST', params)
def revoke_access(self, padId, user, asUser=''):
api_link = 'pad/%s/revoke-access/%s' % (padId, user)
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'POST', params)
def pad_options(self, padId, asUser=''):
api_link = 'pad/%s/options' % padId
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def set_pad_options(self, padId, settings={}, asUser=''):
api_link = 'pad/%s/options' % padId
params = {}
if asUser != '':
params['asUser'] = asUser
for key in settings.keys():
params[key] = settings[key]
return self.do_api_request(api_link, 'POST', params)
def user_settings(self, user, settings={}):
api_link = 'user/%s/settings' % user
params = {}
for key in settings.keys():
params[key] = settings[key]
return self.do_api_request(api_link, 'POST', params)
def user_deletion(self, user):
api_link = 'user/%s/remove' % user
return self.do_api_request(api_link, 'POST')
def user_creation(self, name, email):
api_link = 'user/create'
params = {'email':email, 'name':name}
return self.do_api_request(api_link, 'POST', params)
def list_all(self):
api_link = 'pads/all'
return self.do_api_request(api_link, 'GET')
def site_options(self):
api_link = 'options'
return self.do_api_request(api_link, 'GET')
def do_api_request(self, path, method, params={}, body='', content_type=None):
method = method.upper()
hackpad = {}
if self.sub_domain:
path = urljoin('%s://%s.%s/api/1.0/' % (self.api_scheme, self.sub_domain, self.api_domain), path)
else:
path = urljoin('%s://%s/api/1.0/' % (self.api_scheme, self.api_domain), path)
oauth_params = {
'client_key': self.consumer_key,
'client_secret': self.consumer_secret
}
headers = {}
if content_type:
headers['content-type'] = content_type
hackpad_api = OAuth1Session(**oauth_params)
if method == 'POST':
r = hackpad_api.post(path, data=body.encode('utf-8'), headers=headers, params=params)
hackpad = r.json()
else:
r = hackpad_api.get(path, headers=headers, params=params)
try:
hackpad = r.json()
except:
hackpad = r.content
return hackpad