Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/integration api/users translations accounts #45

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/sxapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import requests

INTEGRATION_API_V2_BASE_URL = "https://api.smaxtec.com/integration/v2"


class ApiTypes(Enum):
PUBLIC = 1
Expand Down Expand Up @@ -90,17 +88,3 @@ def delete(self, path, *args, **kwargs):
url = self.to_url(path)
r = self.session.delete(url, *args, **kwargs)
return r.json()


class IntegrationAPIV2(BaseAPI):
def __init__(self, base_url=None, email=None, password=None, api_token=None):
"""Initialize a new integration api client instance."""
base_url = base_url or INTEGRATION_API_V2_BASE_URL
api_type = ApiTypes.INTEGRATION
super().__init__(
base_url,
email=email,
password=password,
api_token=api_token,
api_type=api_type,
)
2 changes: 1 addition & 1 deletion src/sxapi/cli/cli_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import keyring

from sxapi.base import IntegrationAPIV2
from sxapi.integrationV2 import IntegrationAPIV2
from sxapi.publicV2 import PublicAPIV2


Expand Down
28 changes: 28 additions & 0 deletions src/sxapi/integrationV2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from sxapi.base import (
ApiTypes,
BaseAPI,
)
from sxapi.integrationV2.accounts import Accounts
from sxapi.integrationV2.tranlations import Translations
from sxapi.integrationV2.users import Users

INTEGRATION_API_V2_BASE_URL = "https://api.smaxtec.com/integration/v2"


class IntegrationAPIV2(BaseAPI):
def __init__(self, base_url=None, email=None, password=None, api_token=None):
"""Initialize a new integration api client instance."""
base_url = base_url or INTEGRATION_API_V2_BASE_URL
api_type = ApiTypes.INTEGRATION

self.users = Users(api=self)
self.translations = Translations(api=self)
self.accounts = Accounts(api=self)

super().__init__(
base_url,
email=email,
password=password,
api_token=api_token,
api_type=api_type,
)
95 changes: 95 additions & 0 deletions src/sxapi/integrationV2/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Accounts:
"""
This Class represents the /accounts endpoint fo the IntegrationAPIV2
https://api.smaxtec.com/integration/v2/
"""

def __init__(self, api=None):
self.api = api
self.path_suffix = "/accounts"

def get_usages(self, **kwargs):
"""Get usages of provided account numbers.

Args:
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. List of usages on success,
error message else.

"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + "/usages"
return self.api.get(url_suffix, json=params)

def put(
self,
account_nr,
account_name,
partner_id,
address_name,
street,
county_code,
**kwargs,
):
"""Update an account.

Args:
account_nr (str): Account number of the account
account_name (str): Name of the account
partner_id (str): Partner ID of the account
address_name (str): Name of the address of the account
street (str): Street of the address of the account
county_code (str): Country code of the address of the account
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. Updated account on success,
error message else.

"""
params = {
"account_name": account_name,
"partner_id": partner_id,
"address_name": address_name,
"street": street,
"county_code": county_code,
}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{account_nr}"
return self.api.put(url_suffix, json=params)

def put_organisation(self, account_nr, organisation_id, **kwargs):
"""Add an account to an organisation.

Args:
account_nr (str): Account number of the account
organisation_id (str): Organisation ID of the organisation
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. Result on success,
error message else.

"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{account_nr}/organisation/{organisation_id}"
return self.api.put(url_suffix, json=params)
54 changes: 54 additions & 0 deletions src/sxapi/integrationV2/tranlations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Translations:
"""
This Class represents the /translations endpoint fo the IntegrationAPIV2
https://api.smaxtec.com/integration/v2/
"""

def __init__(self, api=None):
self.api = api
self.path_suffix = "/translations"

def get_events(self, language, **kwargs):
"""Get translations for events.

Args:
language (str): Language of the events to be returned
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. List of events on success,
error message else.

"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{language}/events"
return self.api.get(url_suffix, json=params)

def get_event_types(self, language, event_type, **kwargs):
"""Get translations for event types.

Args:
language (str): Language of the event types to be returned
event_type (str): Type of the event types to be returned
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. List of event types on success,
error message else.

"""
params = {}

for k, v in kwargs.items():
params[k] = v

url_suffix = self.path_suffix + f"/{language}/events/{event_type}"
return self.api.get(url_suffix, json=params)
51 changes: 51 additions & 0 deletions src/sxapi/integrationV2/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Users:
"""
This Class represents the /users endpoint fo the IntegrationAPIV2
https://api.smaxtec.com/integration/v2/
"""

def __init__(self, api=None):
self.api = api
self.path_suffix = "/users"

def get(self, **kwargs):
"""Grant access to demo farm

Args:
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. Demo farm User on success,
error message else.

"""
params = {}

for k, v in kwargs.items():
params[k] = v

return self.api.get(self.path_suffix, json=params)

def post_session_token(self, user, password, **kwargs):
"""Creates a new session token.

Args:
user (str): Email of the user to be logged in
password (str): Password of the user to be logged in
**kwargs: Optional parameters of the API call.
Find supported parameters under
https://api.smaxtec.com/integration/v2/

Returns:
dict: Response of API call. Created session token on success,
error message else.

"""
params = {"user": user, "password": password}

for k, v in kwargs.items():
params[k] = v

return self.api.post(self.path_suffix + "/session_token", json=params)
2 changes: 1 addition & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mock

from sxapi.base import IntegrationAPIV2
from sxapi.integrationV2 import IntegrationAPIV2
from sxapi.publicV2 import PublicAPIV2


Expand Down
65 changes: 65 additions & 0 deletions tests/test_integrationV2/test_intgr_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import mock

from sxapi.integrationV2 import IntegrationAPIV2


@mock.patch("sxapi.integrationV2.IntegrationAPIV2.get")
def test_get_usages(get_mock):
test_api = IntegrationAPIV2()
test_api.accounts.get_usages(kwarg1="kwarg1")

call_args = get_mock.call_args_list[0]

assert get_mock.call_count == 1
assert call_args.args[0] == "/accounts/usages"
assert call_args.kwargs["json"] == {
"kwarg1": "kwarg1",
}


@mock.patch("sxapi.integrationV2.IntegrationAPIV2.put")
def test_put(put_mock):
test_api = IntegrationAPIV2()
test_api.accounts.put(
"test_account_nr",
"test_account_name",
"test_partner_id",
"test_address_name",
"test_street",
"test_county_code",
kwarg1="kwarg1",
)

call_args = put_mock.call_args_list[0]

assert put_mock.call_count == 1
assert call_args.args[0] == "/accounts/test_account_nr"
assert call_args.kwargs["json"] == {
"account_name": "test_account_name",
"partner_id": "test_partner_id",
"address_name": "test_address_name",
"street": "test_street",
"county_code": "test_county_code",
"kwarg1": "kwarg1",
}


@mock.patch("sxapi.integrationV2.IntegrationAPIV2.put")
def test_put_organisation(put_mock):
test_api = IntegrationAPIV2()
test_api.accounts.put_organisation(
"test_account_nr",
"test_organisation_id",
kwarg1="kwarg1",
)

call_args = put_mock.call_args_list[0]

assert put_mock.call_count == 1
assert (
call_args.args[0]
== "/accounts/test_account_nr/organisation/test_organisation_id"
)
assert call_args.kwargs["json"] == {
"kwarg1": "kwarg1",
}
31 changes: 31 additions & 0 deletions tests/test_integrationV2/test_intgr_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mock

from sxapi.integrationV2 import IntegrationAPIV2


@mock.patch("sxapi.integrationV2.IntegrationAPIV2.get")
def test_get_events(get_mock):
test_api = IntegrationAPIV2()
test_api.translations.get_events("en", kwarg1="kwarg1")

call_args = get_mock.call_args_list[0]

assert get_mock.call_count == 1
assert call_args.args[0] == "/translations/en/events"
assert call_args.kwargs["json"] == {
"kwarg1": "kwarg1",
}


@mock.patch("sxapi.integrationV2.IntegrationAPIV2.get")
def test_get_event_types(get_mock):
test_api = IntegrationAPIV2()
test_api.translations.get_event_types("en", "test_event_type", kwarg1="kwarg1")

call_args = get_mock.call_args_list[0]

assert get_mock.call_count == 1
assert call_args.args[0] == "/translations/en/events/test_event_type"
assert call_args.kwargs["json"] == {
"kwarg1": "kwarg1",
}
Loading