Skip to content

Commit b060028

Browse files
committed
Add Arbitrary Methods
1 parent 121599a commit b060028

6 files changed

Lines changed: 128 additions & 8 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Vultr Python
2121

2222
<a title="Vultr Python" href="https://cssnr.github.io/vultr-python" target="_blank">
23-
<img alt="Vultr Python" align="right" width="128" height="auto" src="https://raw.githubusercontent.com/smashedr/repo-images/refs/heads/master/vultr-python/logo128.png"></a>
23+
<img alt="Vultr Python" align="right" width="128" height="auto" src="https://raw.githubusercontent.com/cssnr/vultr-python/refs/heads/master/.github/assets/logo.svg"></a>
2424

2525
- [Install](#Install)
2626
- [Usage](#Usage)
@@ -110,6 +110,17 @@ data = {
110110
instance = vultr.create_instance(**data)
111111
```
112112

113+
Arbitrary Methods `get`, `post`, `patch`, `delete`
114+
115+
```python
116+
# vultr.list_instances()
117+
instances = vultr.get('instances')
118+
# vultr.create_key()
119+
sshkey = vultr.post('ssh-keys', {"name": 'key-name', "ssh_key": 'ssh-rsa AAAA...'})
120+
# vultr.delete_instance()
121+
vultr.delete(f"instances/019ad1a8-2aa3-7650-83d1-8520d65ed6af")
122+
```
123+
113124
Full Documentation: [https://cssnr.github.io/vultr-python](https://cssnr.github.io/vultr-python)
114125

115126
Vultr API Reference: [https://www.vultr.com/api](https://www.vultr.com/api/?ref=6905748)

build.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
param (
2+
[switch]$c,
3+
[switch]$i,
4+
[switch]$u
5+
)
6+
7+
$ErrorActionPreference = "Stop"
8+
9+
write-output "Clean: $c"
10+
write-output "Install: $i"
11+
write-output "Uninstall: $u"
12+
13+
if ($u) {
14+
Write-Host -ForegroundColor Red "Uninstalling..."
15+
python -m pip uninstall -y vultr-python
16+
}
17+
18+
$egg_dir = ".\src\*.egg-info"
19+
if (Test-Path $egg_dir) {
20+
Write-Host -ForegroundColor Cyan "Removing: $egg_dir"
21+
Remove-Item -Force -Recurse $egg_dir
22+
}
23+
$cache_dir = ".\src\*\__pycache__"
24+
if (Test-Path $cache_dir) {
25+
Write-Host -ForegroundColor Cyan "Removing: $cache_dir"
26+
Remove-Item -Force -Recurse $cache_dir
27+
}
28+
if (Test-Path ".\dist") {
29+
Write-Host -ForegroundColor Cyan "Removing: .\dist"
30+
Remove-Item -Force -Recurse ".\dist"
31+
}
32+
if (Test-Path ".\build") {
33+
Write-Host -ForegroundColor Cyan "Removing: .\build"
34+
Remove-Item -Force -Recurse ".\build"
35+
}
36+
if ($c) {
37+
Write-Host -ForegroundColor Yellow "Clean Only. Not Building or Installing!"
38+
exit
39+
}
40+
41+
python -m build
42+
43+
if ($args[0] -eq "i") {
44+
Write-Host -ForegroundColor Green "Installing..."
45+
python -m pip install .\dist\vultr_python-0.0.1-py3-none-any.whl
46+
}
47+
48+
Write-Output "Success."

docs.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
param (
2+
[switch]$c,
3+
[switch]$b
4+
)
5+
6+
$ErrorActionPreference = "Stop"
7+
8+
write-output "Clean: $c"
9+
write-output "Build: $b"
10+
11+
if ($c) {
12+
Write-Host -ForegroundColor Yellow "Cleaning Docs..."
13+
$site_dir = ".\site"
14+
if (Test-Path $site_dir) {
15+
Write-Host -ForegroundColor Cyan "Removing: $site_dir"
16+
Remove-Item -Force -Recurse $site_dir
17+
}
18+
$cache_dir = ".\.cache"
19+
if (Test-Path $cache_dir) {
20+
Write-Host -ForegroundColor Cyan "Removing: $cache_dir"
21+
Remove-Item -Force -Recurse $cache_dir
22+
}
23+
}
24+
25+
python -m pdoc -t .\docs\ -p 8008 `
26+
--favicon "https://df.cssnr.com/raw/logo128.png" `
27+
--logo "https://df.cssnr.com/raw/logo128.png" `
28+
--logo-link "https://github.com/cssnr/vultr-python" `
29+
vultr
30+
31+
#-e "vultr=https://github.com/cssnr/vultr-python/blob/updates/src/vultr/" `
32+
33+
#if ($b) {
34+
# Write-Host -ForegroundColor Green "Building Docs..."
35+
# zensical build
36+
#} else {
37+
# zensical serve
38+
#}

docs/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ data = {
8888
instance = vultr.create_instance(**data)
8989
```
9090

91+
Arbitrary Methods `get`, `post`, `patch`, `delete`
92+
93+
```python
94+
# vultr.list_instances()
95+
instances = vultr.get('instances')
96+
# vultr.create_key()
97+
sshkey = vultr.post('ssh-keys', {"name": 'key-name', "ssh_key": 'ssh-rsa AAAA...'})
98+
# vultr.delete_instance()
99+
vultr.delete(f"instances/019ad1a8-2aa3-7650-83d1-8520d65ed6af")
100+
```
101+
91102
&nbsp;
92103

93104
Vultr API Reference: [https://www.vultr.com/api](https://www.vultr.com/api/?ref=6905748)

src/vultr/py.typed

Whitespace-only changes.

src/vultr/vultr.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ class Vultr(object):
99

1010
def __init__(self, api_key: Optional[str] = None):
1111
"""
12-
:param str api_key: Vultr API Key or VULTR_API_KEY environment variable
12+
:param str api_key: Vultr API Key or VULTR_API_KEY Environment Variable
1313
"""
1414
self.api_key = api_key or os.getenv("VULTR_API_KEY")
15-
self.s = requests.session()
15+
self._session = requests.session()
1616
if self.api_key:
17-
self.s.headers.update({"Authorization": f"Bearer {self.api_key}"})
17+
self._session.headers.update({"Authorization": f"Bearer {self.api_key}"})
18+
19+
def get(self, url: str):
20+
return self._get(f"{self.url}/{url.lstrip('/')}")
21+
22+
def post(self, url: str, **kwargs):
23+
return self._post(f"{self.url}/{url.lstrip('/')}", kwargs)
24+
25+
def patch(self, url: str, **kwargs):
26+
return self._patch(f"{self.url}/{url.lstrip('/')}", kwargs)
27+
28+
def delete(self, url: str):
29+
return self._delete(f"{self.url}/{url.lstrip('/')}")
1830

1931
def list_os(self):
2032
url = f"{self.url}/os"
@@ -144,25 +156,25 @@ def filter_regions(regions: list, locations: list) -> list:
144156
return [d for d in regions if d["id"] in locations]
145157

146158
def _get(self, url):
147-
r = self.s.get(url, timeout=10)
159+
r = self._session.get(url, timeout=10)
148160
if not r.ok:
149161
r.raise_for_status()
150162
return r.json()
151163

152164
def _post(self, url, data):
153-
r = self.s.post(url, json=data, timeout=10)
165+
r = self._session.post(url, json=data, timeout=10)
154166
if not r.ok:
155167
r.raise_for_status()
156168
return r.json()
157169

158170
def _patch(self, url, data):
159-
r = self.s.patch(url, json=data, timeout=10)
171+
r = self._session.patch(url, json=data, timeout=10)
160172
if not r.ok:
161173
r.raise_for_status()
162174
return r.json()
163175

164176
def _delete(self, url):
165-
r = self.s.delete(url, timeout=10)
177+
r = self._session.delete(url, timeout=10)
166178
if not r.ok:
167179
r.raise_for_status()
168180
return None

0 commit comments

Comments
 (0)