Skip to content

Commit

Permalink
[CODE QUALITY]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Dec 8, 2023
1 parent a0c4d4f commit da0e07b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@ Deploy your autonomous agents to the cloud with infinite scalability, 99% uptime

Base swarms code -> fastapi code is added to top and bottom -> agent is decorated with expose as api decorator -> file is parsed and then -> added onto yaml for -> skypilot


# Stack
- Backend: FastAPI
- Skypilot for container management
- Stripe for payment tracking
- Postresql for database



# License
MIT
35 changes: 35 additions & 0 deletions swarms_cloud/api_key_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import secrets
import string


def generate_api_key(prefix="sk-", length: int = 50):
"""Generate a random api key
Args:
prefix (str, optional): _description_. Defaults to "sk-".
length (int, optional): _description_. Defaults to 50.
Raises:
ValueError: _description_
RuntimeError: _description_
Returns:
_type_: _description_
Example:
>>> from swarms_cloud.api_key_generator import generate_api_key
>>> generate_api_key()
>>> sk-9a7b8c5d6e7f8g9h0i1j2k3l4m5n6o7p8q9r0s1t2u3v4w5x6y7z8A9B0C1D2E3F4G5H6I7J8K9L0M1N2O3P4Q5R6S7T8U9V0W1X2Y3Z4
"""
if length <= len(prefix):
raise ValueError("Length must be greater than prefix length")

try:
# Generate a random string of letters for the api
characters = string.ascii_letters + string.digits
secure_key = "".join(secrets.choice(characters) for _ in range(length))
return prefix + secure_key
except Exception as error:
# Handle unexpeccted errors
raise RuntimeError(f"Error generating api key: {error}")
101 changes: 101 additions & 0 deletions tests/test_api_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import pytest
from swarms_cloud.api_key_generator import generate_api_key


# Basic tests
def test_generate_api_key_default():
api_key = generate_api_key()
assert isinstance(api_key, str)
assert api_key.startswith("sk-")
assert len(api_key) == 52 # Prefix (3) + 50 random characters


def test_generate_api_key_custom_length():
api_key = generate_api_key(length=10)
assert len(api_key) == 13 # Prefix (3) + 10 random characters


def test_generate_api_key_custom_prefix():
api_key = generate_api_key(prefix="custom-")
assert api_key.startswith("custom-")
assert len(api_key) == 57 # Custom prefix (7) + 50 random characters


# Exception tests
def test_generate_api_key_short_length():
with pytest.raises(ValueError):
generate_api_key(length=2)


def test_generate_api_key_unexpected_error():
with pytest.raises(RuntimeError):
generate_api_key(length=1000000)


# Parameterized tests
@pytest.mark.parametrize(
"prefix, length",
[
("pre-", 15),
("test-", 25),
("longprefix-", 100),
],
)
def test_generate_api_key_parameterized(prefix, length):
api_key = generate_api_key(prefix=prefix, length=length)
assert api_key.startswith(prefix)
assert len(api_key) == len(prefix) + length


# Test performance (optional)
def test_generate_api_key_performance(benchmark):
benchmark(generate_api_key)


# Additional tests
def test_generate_api_key_invalid_prefix_type():
with pytest.raises(ValueError):
generate_api_key(prefix=123)


def test_generate_api_key_negative_length():
with pytest.raises(ValueError):
generate_api_key(length=-10)


def test_generate_api_key_empty_prefix():
api_key = generate_api_key(prefix="")
assert api_key.startswith("sk-")
assert len(api_key) == 53 # Prefix (3) + 50 random characters


def test_generate_api_key_special_characters_prefix():
api_key = generate_api_key(prefix="@#$")
assert api_key.startswith("@#$")
assert len(api_key) == 53 # Special prefix (3) + 50 random characters


def test_generate_api_key_prefix_length():
api_key = generate_api_key(prefix="x" * 100)
assert len(api_key) == 150 # Custom prefix (100) + 50 random characters


def test_generate_api_key_zero_length():
with pytest.raises(ValueError):
generate_api_key(length=0)


def test_generate_api_key_zero_length_custom_prefix():
with pytest.raises(ValueError):
generate_api_key(prefix="custom-", length=0)


def test_generate_api_key_negative_length_custom_prefix():
with pytest.raises(ValueError):
generate_api_key(prefix="custom-", length=-10)


def test_generate_api_key_long_prefix():
api_key = generate_api_key(prefix="verylongprefix-")
assert api_key.startswith("verylongprefix-")
assert len(api_key) == 63 # Custom prefix (16) + 50 random characters
2 changes: 1 addition & 1 deletion tests/test_rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import FastAPI, HTTPException
from time import time
import logging
from swarms_cloud.main import rate_limiter
from swarms_cloud.rate_limiter import rate_limiter


class MockClient:
Expand Down

0 comments on commit da0e07b

Please sign in to comment.