Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
loldruger committed Jan 20, 2025
1 parent 3826117 commit a6863e6
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Python Debugger: Module",
"type": "debugpy",
"request": "launch",
"module": "vultr_py"
"module": "src.vultr_py"
},
{
"name": "Python Debugger: Python File",
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
python-dotenv = "*"
aiohttp = "*"

[dev-packages]

Expand Down
489 changes: 488 additions & 1 deletion Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# vultr_py
[WIP] Vultr API wrapper for Infrastructre as Code
[WIP] Vultr API v2 Fully Documented, Typed and Async Wrapper for Infrastructre as Code
4 changes: 4 additions & 0 deletions src/vultr_py/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import http
import http.client
from . import composer

17 changes: 17 additions & 0 deletions src/vultr_py/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import asyncio
from http import HTTPMethod

import src.vultr_py as vultr_py
import src.vultr_py.composer as composer

async def main():
await composer.Request(composer.URL_ACCOUNT)\
.set_method(HTTPMethod.GET)\
.add_header("Authorization", "Bearer {token}")\
.add_header("Content-Type", "application/json")\
.set_body({
"key": "value"
})\
.request()

asyncio.run(main())
96 changes: 96 additions & 0 deletions src/vultr_py/composer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import aiohttp

from typing import Final
from enum import IntEnum, auto
from http import HTTPMethod

class Provider(IntEnum):
VULTR = auto()
CUSTOM = auto()

def __str__(self) -> str:
match self:
case Provider.VULTR:
return "https://api.vultr.com/v2/"
case Provider.CUSTOM:
return "https://api.custom.com/v2/"
case _:
raise ValueError("Invalid provider")

class Url:
def __init__(self, provider: Provider):
self._provider = provider
self._uri = ""

def uri(self, token: str) -> 'Url':
self._uri = token

return self

def assign(self, placeholder: str, value: str) -> 'Url':
self._uri = self._uri.replace(f"{{{placeholder}}}", value)

return self

def to_str(self) -> str:
return str(self._provider) + self._uri

class Request:
def __init__(self, url: Url):
self._url = url.to_str()
self._method = None
self._headers = {}
self._params = {}
self._body = None

def set_method(self, method: HTTPMethod) -> 'Request':
self._method = str(method)

return self

def add_header(self, key: str, value: str) -> 'Request':
self._headers[key] = value

return self

def add_param(self, key: str, value: str) -> 'Request':
self._params[key] = value

return self

def set_body(self, body: dict) -> 'Request':
self._body = body

return self

async def request(self) -> dict:
async with aiohttp.ClientSession() as session:
async with session.request(
method=self._method,
url=self._url,
headers=self._headers,
params=self._params,
json=self._body
) as response:
return await response.json()


URL_ACCOUNT_BANDWIDTH: Final[Url] = Url(Provider.VULTR).uri("account/bandwidth")

URL_APPLICATIONS: Final[Url] = Url(Provider.VULTR).uri("applications")

URL_BACKUPS: Final[Url] = Url(Provider.VULTR).uri("backups")

URL_BARE_METAL_IP4: Final[Url] = Url(Provider.VULTR).uri("bare-metal/{baremetal-id}/ipv4")
URL_BARE_METAL_IP6: Final[Url] = Url(Provider.VULTR).uri("bare-metal/{baremetal-id}/ipv6")

URL_ACCOUNT: Final[Url] = Url(Provider.VULTR).uri("account")

REQUEST_ACCOUNT: Final[Request] = Request(URL_ACCOUNT)\
.set_method(HTTPMethod.GET)\
.add_header("Authorization", "Bearer {token}")\
.add_header("Content-Type", "application/json")\
.set_body({
"key": "value"
})\
.request()
12 changes: 12 additions & 0 deletions src/vultr_py/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Final

URL: Final[str] = "https://api.vultr.com/v2/"

ACCOUNT: Final[str] = "account"
ACCOUNT_BANDWIDTH: Final[str] = "account/bandwidth"

APPLICATIONS: Final[str] = "applications"

BACKUPS: Final[str] = "backups"

BARE_METAL: Final[str] = "bare-metal"
6 changes: 0 additions & 6 deletions vultr_py/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions vultr_py/__main__.py

This file was deleted.

0 comments on commit a6863e6

Please sign in to comment.