Compare commits
3 Commits
5b4999f12a
...
be21a1ef4a
Author | SHA1 | Date |
---|---|---|
Nick Chambers | be21a1ef4a | |
Nick Chambers | 1e8a3c3517 | |
Nick Chambers | 7ffc691a7b |
|
@ -1,4 +1,21 @@
|
|||
import requests
|
||||
import vultron.err
|
||||
|
||||
class BadCreds(vultron.err.Error):
|
||||
def __str__(self):
|
||||
return f"api key supplied is not valid"
|
||||
|
||||
class BadReq(vultron.err.Error):
|
||||
def __str__(self):
|
||||
return f"received unexpected error code: {self.args[0]}"
|
||||
|
||||
class RateLimit(vultron.err.Error):
|
||||
def __str__(self):
|
||||
return f"you are currently being rate limited by Vultr"
|
||||
|
||||
class VultrIssue(vultron.err.Error):
|
||||
def __str__(self):
|
||||
return f"request could not be processed by Vultr"
|
||||
|
||||
class Client:
|
||||
DEFAULT_ENDPOINT = "https://api.vultr.com/v2"
|
||||
|
@ -10,6 +27,7 @@ class Client:
|
|||
self.endpoint = self.DEFAULT_ENDPOINT
|
||||
|
||||
self.hdrs = {
|
||||
# FIXME: Find way to automatically get version
|
||||
"User-Agent": "Vultron",
|
||||
"Authorization": f"Bearer {api_key}"
|
||||
}
|
||||
|
@ -18,8 +36,15 @@ class Client:
|
|||
url = f"{self.endpoint}/{'/'.join(resources)}"
|
||||
res = requests.get(url, headers=self.hdrs)
|
||||
|
||||
if res.status_code == 200:
|
||||
if res.status_code in (200, 202):
|
||||
return res.json()
|
||||
elif res.status_code == 204:
|
||||
return None
|
||||
elif res.status_code == 401:
|
||||
raise BadCreds()
|
||||
elif res.status_code == 429:
|
||||
raise RateLimit()
|
||||
elif res.status_code == 500:
|
||||
raise VultrIssue()
|
||||
else:
|
||||
pass
|
||||
# FIXME: raise error w/ status code here
|
||||
raise BadReq(res.status_code)
|
||||
|
|
|
@ -5,6 +5,5 @@ class Help(vultron.cmd.Command):
|
|||
def init(self):
|
||||
self.uses_api = False
|
||||
|
||||
def vultron_help(self, *args):
|
||||
# FIXME: print global help message
|
||||
pass
|
||||
def vultron_help(self):
|
||||
print("stub help message here")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
class OutFormat:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.spans = { }
|
||||
self.rows = [ ]
|
||||
|
||||
|
@ -25,8 +26,7 @@ class OutFormat:
|
|||
return str(field)
|
||||
|
||||
def render(self):
|
||||
# FIXME: consider if this should do anything
|
||||
pass
|
||||
return self.data
|
||||
|
||||
class Basic(OutFormat):
|
||||
def __init__(self, data):
|
||||
|
|
Loading…
Reference in New Issue