Skip to content

Commit

Permalink
Merge pull request #32 from jadolg/add-id-create
Browse files Browse the repository at this point in the history
Add support for key creation with ID (new in 1.8.0)
  • Loading branch information
jadolg authored Feb 5, 2024
2 parents 19579e3 + 9150ab6 commit ad423c0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
46 changes: 28 additions & 18 deletions outline_vpn/outline_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import requests
from urllib3 import PoolManager

UNABLE_TO_GET_METRICS_ERROR = "Unable to get metrics"


@dataclass
class OutlineKey:
Expand Down Expand Up @@ -76,10 +78,10 @@ def get_keys(self):
f"{self.api_url}/metrics/transfer", verify=False
)
if (
response_metrics.status_code >= 400
or "bytesTransferredByUserId" not in response_metrics.json()
response_metrics.status_code >= 400
or "bytesTransferredByUserId" not in response_metrics.json()
):
raise OutlineServerErrorException("Unable to get metrics")
raise OutlineServerErrorException(UNABLE_TO_GET_METRICS_ERROR)

response_json = response.json()
result = []
Expand Down Expand Up @@ -112,10 +114,10 @@ def get_key(self, key_id: str) -> OutlineKey:
f"{self.api_url}/metrics/transfer", verify=False
)
if (
response_metrics.status_code >= 400
or "bytesTransferredByUserId" not in response_metrics.json()
response_metrics.status_code >= 400
or "bytesTransferredByUserId" not in response_metrics.json()
):
raise OutlineServerErrorException("Unable to get metrics")
raise OutlineServerErrorException(UNABLE_TO_GET_METRICS_ERROR)

outline_key = OutlineKey(
key_id=key.get("id"),
Expand All @@ -134,11 +136,12 @@ def get_key(self, key_id: str) -> OutlineKey:
raise OutlineServerErrorException("Unable to get key")

def create_key(
self,
name: str = None,
method: str = None,
password: str = None,
data_limit: int = None,
self,
key_id: str = None,
name: str = None,
method: str = None,
password: str = None,
data_limit: int = None,
) -> OutlineKey:
"""Create a new key"""

Expand All @@ -152,9 +155,16 @@ def create_key(
if data_limit:
payload["limit"] = {"bytes": data_limit}

response = self.session.post(
f"{self.api_url}/access-keys", verify=False, json=payload
)
if key_id:
payload["id"] = key_id
response = self.session.put(
f"{self.api_url}/access-keys/{key_id}", verify=False, json=payload
)
else:
response = self.session.post(
f"{self.api_url}/access-keys", verify=False, json=payload
)

if response.status_code == 201:
key = response.json()
outline_key = OutlineKey(
Expand All @@ -165,7 +175,7 @@ def create_key(
method=key.get("method"),
access_url=key.get("accessUrl"),
used_bytes=0,
data_limit=key.get("dataLimit", {}).get("bytes")
data_limit=key.get("dataLimit", {}).get("bytes"),
)
return outline_key

Expand Down Expand Up @@ -216,10 +226,10 @@ def get_transferred_data(self):
}"""
response = self.session.get(f"{self.api_url}/metrics/transfer", verify=False)
if (
response.status_code >= 400
or "bytesTransferredByUserId" not in response.json()
response.status_code >= 400
or "bytesTransferredByUserId" not in response.json()
):
raise OutlineServerErrorException("Unable to get metrics")
raise OutlineServerErrorException(UNABLE_TO_GET_METRICS_ERROR)
return response.json()

def get_server_information(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="outline-vpn-api",
version="6.0.0",
version="6.1.0",
packages=["outline_vpn"],
url="https://github.com/jadolg/outline-vpn-api/",
license="MIT",
Expand Down
17 changes: 17 additions & 0 deletions test_outline_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ def test_create_key_with_attributes(client: OutlineVPN):
assert client.delete_key(key.key_id)


def test_create_key_with_attributes_and_id(client: OutlineVPN):
"""Test creating a key with attributes and id"""
key = client.create_key(
key_id="test-id",
name="Yet another test key",
data_limit=1024 * 1024 * 20,
method="aes-192-gcm",
password="test",
)
assert key.key_id == "test-id"
assert key.name == "Yet another test key"
assert key.method == "aes-192-gcm"
assert key.password == "test"
assert key.data_limit == 1024 * 1024 * 20
assert client.delete_key(key.key_id)


def test_limits(client: OutlineVPN): # pylint: disable=W0621
"""Test setting, retrieving and removing custom limits"""
new_limit = 1024 * 1024 * 20
Expand Down

0 comments on commit ad423c0

Please sign in to comment.