-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvercel.py
92 lines (66 loc) · 2.8 KB
/
vercel.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
84
85
86
87
88
89
90
91
92
"""Vercel Module"""
import requests
from cache import Cache
class Client(object):
"""CLass that interacts with Vercel API"""
API_PROJECTS_URL = "https://api.vercel.com/v9/projects/"
CACHE_TTL = 3600 # 1h
CACHE_KEY = "vercel_sites"
def __init__(self, access_token, logger):
""" Class constructor """
self.access_token = access_token
self.logger = logger
self.username = ""
self.team_slug = ""
def set_access_token(self, access_token):
""" Sets the Access Token """
self.access_token = access_token
def set_username(self, username):
""" Sets the Username """
self.username = username
def set_team_slug(self, team_slug):
""" Sets the Team Slug """
self.team_slug = team_slug
def get_team_slug(self):
""" Gets the Team Slug """
return self.team_slug
def get_username(self):
""" Gets the Username """
return self.username
def filter_sites(self, sites, filter_term=None):
"""Filter the sites returned by Vercel, by the name passed in filter parameter"""
if not filter_term:
return sites
filtered_sites = []
for site in sites:
production_targets = site.get('targets', {}).get('production', {})
aliases = production_targets.get('alias', [])
if filter_term.lower() in site['name'].lower() or (filter_term.lower() in aliases) or any(filter_term.lower() in alias.lower() for alias in aliases):
filtered_sites.append(site)
return filtered_sites
def get_sites(self, filter_term=None):
"""Gets a list of user sites from Vercel"""
self.logger.debug("getting sites from Vercel")
if Cache.get(self.CACHE_KEY):
self.logger.debug("Loading from cache")
return self.filter_sites(Cache.get(self.CACHE_KEY), filter_term)
headers = {
"Authorization": "Bearer {}".format(
self.access_token),
"Accept": "application/json",
"User-Agent": "Ulauncher-Vercel"}
req = requests.get(self.API_PROJECTS_URL, headers=headers)
if not req.ok:
if req.status_code == 401:
raise AuthenticationException(
"Failed to authenticate with access token " + self.access_token)
raise GenericException(
"Error connecting to Vercel API : status " + req.status_code)
data = req.json()
data = data.get('projects', [])
Cache.set(self.CACHE_KEY, data, self.CACHE_TTL)
return self.filter_sites(data, filter_term)
class GenericException(Exception):
""" Generic Exception """
class AuthenticationException(Exception):
""" Exception thrown when the Authentication on Vercel fails """