Skip to content

Commit

Permalink
Fix connection error (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 authored Jun 25, 2024
1 parent 3a390c4 commit 2b2740d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
26 changes: 18 additions & 8 deletions apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from apps.mappings.models import Version
from apps.mappings.exceptions import handle_import_exceptions

from sage_desktop_sdk.exceptions import (
UserAccountLocked,
InvalidUserCredentials,
InvalidWebApiClientCredentials,
WebApiClientLocked,
SageDesktopSDKError
)

logger = logging.getLogger(__name__)
logger.level = logging.INFO

Expand All @@ -22,14 +30,16 @@ def __init__(self, credentials_object: Sage300Credential, workspace_id: int):
:param credentials_object: Sage300Credential instance containing API credentials
:param workspace_id: ID of the workspace
"""

self.connection = SageDesktopSDK(
api_key=credentials_object.api_key,
api_secret=credentials_object.api_secret,
user_name=credentials_object.username,
password=credentials_object.password,
indentifier=credentials_object.identifier
)
try:
self.connection = SageDesktopSDK(
api_key=credentials_object.api_key,
api_secret=credentials_object.api_secret,
user_name=credentials_object.username,
password=credentials_object.password,
indentifier=credentials_object.identifier
)
except (InvalidUserCredentials, InvalidWebApiClientCredentials, UserAccountLocked, WebApiClientLocked, SageDesktopSDKError) as e:
logger.error(f'Error while connecting to Sage300: {e}')

self.workspace_id = workspace_id

Expand Down
34 changes: 19 additions & 15 deletions sage_desktop_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,35 @@ def update_cookie(self, api_key: str, api_secret: str):
'Content-type': 'application/json',
}

api_data = json.dumps({
"ApiKey": api_key,
"ApiSecret": api_secret,
"Password": self.__user_password,
api_data = json.dumps({
"ApiKey": api_key,
"ApiSecret": api_secret,
"Password": self.__user_password,
"Username": self.__user_id
})

authentication_url = self.__api_url + '/Api/Security/V3/Session.svc/authenticate'
result = requests.request("POST", url=authentication_url, headers=request_header, data=api_data)
response = json.loads(result.text)
try:
response = json.loads(result.text)

if response['Result'] == 5:
self.__cookie = result.headers.get('Set-Cookie')
if response['Result'] == 5:
self.__cookie = result.headers.get('Set-Cookie')

if response['Result'] == 1:
raise InvalidUserCredentials('Invalid User Credentials')
if response['Result'] == 1:
raise InvalidUserCredentials('Invalid User Credentials')

if response['Result'] == 2:
raise InvalidWebApiClientCredentials('Invalid Webapp Client')
if response['Result'] == 2:
raise InvalidWebApiClientCredentials('Invalid Webapp Client')

if response['Result'] == 3:
raise UserAccountLocked('User Account Locked')
if response['Result'] == 3:
raise UserAccountLocked('User Account Locked')

if response['Result'] == 4:
raise WebApiClientLocked('Web API client Locked')
if response['Result'] == 4:
raise WebApiClientLocked('Web API client Locked')

except Exception as e:
raise SageDesktopSDKError("Error while connecting with hh2 | {0}".format(e))

def _query_get_all_generator(self, url: str, is_paginated: bool = False) -> Generator[Dict, None, None]:
"""
Expand Down

0 comments on commit 2b2740d

Please sign in to comment.