forked from chaosct/repovizz2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repovizz.py
83 lines (70 loc) · 2.77 KB
/
repovizz.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
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
import os
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
from requests_oauthlib import OAuth2Session
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from time import sleep
SCRIPT_PORT = 50009
REPOVIZZ_URL = "http://repovizz2.upf.edu"
SCRIPT_URL = "http://localhost:{}".format(SCRIPT_PORT)
class RepoVizzClient(object):
def __init__(self, client_id, client_secret, repovizz_url=REPOVIZZ_URL):
self.client_id = client_id
self.client_secret = client_secret
self.repovizz_url = repovizz_url
self.miniserver = None
self.request_data = None
self.oauthclient = None
self.authenticated = True
def start_auth(self, async=False):
self.request_data = None
# self.start_server(async)
return self.get_auth_url()
def start_server(self, async=True):
if not async:
self._mini_server_main()
else:
if not self.miniserver:
self.miniserver = Thread(target=self._mini_server_main,args=(async,))
self.miniserver.daemon = True
self.miniserver.start()
def _mini_server_main(self, async=True):
s = socket(AF_INET, SOCK_STREAM)
s.bind(('127.0.0.1', SCRIPT_PORT))
s.listen(1)
response = 'HTTP/1.0 200 OK\nConnection: close\nContent-Length: 10\nContent-Type: text/html\n\nThank you.'
while True:
(conn, addr) = s.accept()
self.request_data = conn.recv(1024)
conn.send(response)
conn.close()
if not async:
break
s.close()
def get_auth_url(self):
authorization_base_url = self.repovizz_url + '/oauth/authorize'
redirect_uri = SCRIPT_URL
scope = ['basic']
self.oauthclient = OAuth2Session(self.client_id, scope=scope, redirect_uri=redirect_uri)
authorization_url, state = self.oauthclient.authorization_url(authorization_base_url)
return authorization_url
def finish_auth(self):
while not self.request_data:
sleep(0.1)
url_auth_with_token = 'http://'+self.request_data.split()[4]+self.request_data.split()[1]
token_url = self.repovizz_url + "/oauth/token"
self.oauthclient.fetch_token(
token_url,
client_secret=self.client_secret,
authorization_response=url_auth_with_token)
self.authenticated = True
def get(self, url, raw=False, *args, **kwargs):
r = self.oauthclient.get(self.repovizz_url+url, *args, **kwargs)
if raw:
return r
return r.json()
def post(self, url, raw=False, *args, **kwargs):
r = self.oauthclient.post(self.repovizz_url+url, *args, **kwargs)
if raw:
return r
return r.json()