Skip to content

Commit

Permalink
Fix the exception import and init function fixes (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Aravinda Vishwanathapura <[email protected]>
  • Loading branch information
aravindavk authored Dec 5, 2023
1 parent 0364d9a commit 7504576
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 55 deletions.
36 changes: 6 additions & 30 deletions python/kadalu_content_apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@
from kadalu_content_apis.folders import Folder
from kadalu_content_apis.objects import Document
from kadalu_content_apis.templates import Template
from kadalu_content_apis.helpers import ConnectionBase, APIError, json_from_response
from kadalu_content_apis.helpers import ConnectionBase, APIError, json_from_response, ExistsError, NotFoundError

DEFAULT_URL = "https://app.kadalu.tech"

class Connection(ConnectionBase):
def __init__(self, url, username=None, email=None, password=None, api_key=None):
def __init__(self, username, api_key, url=DEFAULT_URL):
"""Intialise Connection and Login to Kadalu Content API"""
self.url = url.strip("/")
super().__init__()

if username is not None and password is not None:
resp = self.http_post(self.url + "/api/api-keys", {"username": username, "password": password})

# TODO: Send correct error response from API when username/password/etc are wrong
# Currently response data seems to be empty.
# if resp.status != 201:
# raise APIError(resp)

resp_json = json_from_response(resp)

self.username = username
self.api_key = resp_json["token"]

if email is not None and password is not None:
resp = self.http_post(self.url + "/api/api-keys", {"email": email, "password": password})

# TODO: Send correct error response from API when email/password/etc are wrong
# Currently response data seems to be empty.
# if resp.status != 201:
# raise APIError(resp)

resp_json = json_from_response(resp)

self.username = username
self.api_key = resp_json["token"]

if username is not None and api_key is not None:
self.username = username
self.api_key = api_key
Expand Down Expand Up @@ -72,9 +48,9 @@ def from_env_file(cls, env_file_path):
env_vars[key.strip()] = value.strip()

return Connection(
env_vars["URL"],
username=env_vars["USERNAME"],
api_key=env_vars["API_KEY"]
api_key=env_vars["API_KEY"],
url=env_vars.get("URL", DEFAULT_URL)
)

def create_region(self, name, address):
Expand Down
26 changes: 1 addition & 25 deletions python/tests/kadalu_content_api_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,11 @@
import kadalu_content_apis

USERNAME = os.environ.get("USERNAME")
EMAIL = os.environ.get("EMAIL")
PASSWORD = os.environ.get("PASSWORD")
API_KEY = os.environ.get("API_KEY")
URL = "http://localhost:5001"


def test_username_password_login():
conn = kadalu_content_apis.Connection(
url=URL,
username=USERNAME,
password=PASSWORD
)

# Token and user_id is set after making connection
assert conn.api_key != ""
assert conn.username == USERNAME

def test_email_password_login():
conn = kadalu_content_apis.Connection(
url=URL,
email=EMAIL,
password=PASSWORD
)
# Token and user_id is set after making connection
assert conn.api_key != ""
assert conn.username == USERNAME


def test_user_id_token_login():
def test_username_token_login():
conn = kadalu_content_apis.Connection(
url=URL,
username=USERNAME,
Expand Down

0 comments on commit 7504576

Please sign in to comment.