Skip to content
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
2 changes: 0 additions & 2 deletions shipengine_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import logging
from logging import NullHandler

from .models import * # noqa

# SDK imports here
from .shipengine import ShipEngine
from .shipengine_config import ShipEngineConfig
Expand Down
3 changes: 3 additions & 0 deletions shipengine_sdk/models/enums/error_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ class ErrorType(Enum):

AUTHORIZATION = "authorization"
"""General authorization error type."""

ERROR = "error"
"""Generic error."""
9 changes: 9 additions & 0 deletions shipengine_sdk/services/address_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ..models.address import Address, AddressValidateResult
from ..models.enums import RPCMethods
from ..shipengine_config import ShipEngineConfig
from ..util import does_normalized_address_have_errors


def validate(address: Address, config: ShipEngineConfig) -> AddressValidateResult:
Expand Down Expand Up @@ -33,5 +34,13 @@ def validate(address: Address, config: ShipEngineConfig) -> AddressValidateResul


def normalize(address: Address, config: ShipEngineConfig) -> Address:
"""
Normalize a given address into a standardized format.

:param Address address: The address to be validate.
:param ShipEngineConfig config: The global ShipEngine configuration object.
:returns: :class:`Address`: The normalized address returned from ShipEngine API.
"""
validation_result: AddressValidateResult = validate(address=address, config=config)
does_normalized_address_have_errors(result=validation_result)
return validation_result.normalized_address
9 changes: 8 additions & 1 deletion shipengine_sdk/shipengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict, Union

from .models.address import Address, AddressValidateResult
from .services.address_validation import validate
from .services.address_validation import normalize, validate
from .shipengine_config import ShipEngineConfig


Expand Down Expand Up @@ -40,3 +40,10 @@ def validate_address(
"""
config: ShipEngineConfig = self.config.merge(new_config=config)
return validate(address, config)

def normalize_address(
self, address: Address, config: Union[Dict[str, any], ShipEngineConfig] = None
) -> Address:
"""Normalize a given address into a standardized format used by carriers."""
config: ShipEngineConfig = self.config.merge(new_config=config)
return normalize(address=address, config=config)
37 changes: 37 additions & 0 deletions shipengine_sdk/util/sdk_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,40 @@ def is_response_500(status_code: int, response_body: Dict[str, any]) -> None:
error_type=error_data["type"],
error_code=error_data["code"],
)


def does_normalized_address_have_errors(result) -> None:
"""
Assertions to check if the returned normalized address has any errors. If errors
are present an exception is thrown.

:param AddressValidateResult result: The address validation response from ShipEngine API.
"""
if len(result.errors) > 1:
error_list = list()
map(lambda msg: error_list.append(msg), result.errors)
str_errors = "\n".join(error_list)

raise ShipEngineError(
message=f"Invalid address.\n {str_errors}",
request_id=result.request_id,
source=ErrorSource.SHIPENGINE.value,
error_type=ErrorType.ERROR.value,
error_code=ErrorCode.INVALID_ADDRESS.value,
)
elif len(result.errors) == 1:
raise ShipEngineError(
message=f"Invalid address.\n {result.errors[0]['message']}",
request_id=result.request_id,
source=ErrorSource.SHIPENGINE.value,
error_type=ErrorType.ERROR.value,
error_code=result.errors[0]["code"],
)
elif result.is_valid is False:
raise ShipEngineError(
message="Invalid address - The address provided could not be normalized.",
request_id=result.request_id,
source=ErrorSource.SHIPENGINE.value,
error_type=ErrorType.ERROR.value,
error_code=ErrorCode.INVALID_ADDRESS.value,
)
2 changes: 1 addition & 1 deletion tests/jsonrpc/test_process_request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Testing the process request and response functions."""
import pytest

from shipengine_sdk import ErrorCode, ErrorSource, ErrorType, RPCMethods
from shipengine_sdk.errors import (
AccountStatusError,
BusinessRuleError,
Expand All @@ -11,6 +10,7 @@
ValidationError,
)
from shipengine_sdk.jsonrpc import handle_response, wrap_request
from shipengine_sdk.models import ErrorCode, ErrorSource, ErrorType, RPCMethods


def handle_response_errors(error_source: str, error_code: str, error_type: str):
Expand Down
1 change: 1 addition & 0 deletions tests/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Initial Docstring"""
Loading