Skip to content

Commit

Permalink
chore: improve enum typing for lang and language configs for connecto…
Browse files Browse the repository at this point in the history
…rs and handle default values for carrier registration modal
  • Loading branch information
danh91 committed Aug 22, 2024
1 parent f7c1be3 commit 5a40c51
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""Karrio Canada post client settings."""

import attr
from karrio.providers.canadapost import Settings as BaseSettings
import karrio.lib as lib
import karrio.providers.canadapost.utils as utils


@attr.s(auto_attribs=True)
class Settings(BaseSettings):
class Settings(utils.Settings):
"""Canada post connection settings."""

username: str
password: str
customer_number: str = None
contract_id: str = None
language: str = "en"
language: utils.LanguageEnum = "en" # type: ignore

id: str = None
test_mode: bool = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import karrio.core.settings as settings


LanguageEnum = lib.units.create_enum("LanguageEnum", ["en", "fr"])


class Settings(settings.Settings):
"""Canada post connection settings."""

username: str
password: str
customer_number: str
contract_id: str = None
language: str = "en"

id: str = None
account_country_code: str = "CA"
metadata: dict = {}
language: LanguageEnum = "en" # type: ignore

@property
def carrier_name(self):
Expand Down
6 changes: 3 additions & 3 deletions modules/connectors/canpar/karrio/mappers/canpar/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Karrio Canpar client settings."""

import attr
from karrio.providers.canpar import Settings as BaseSettings
import karrio.providers.canpar.utils as utils


@attr.s(auto_attribs=True)
class Settings(BaseSettings):
class Settings(utils.Settings):
"""Canpar connection settings."""

username: str
password: str
language: str = "en"
language: utils.LanguageEnum = "en" # type: ignore

id: str = None
test_mode: bool = False
Expand Down
11 changes: 7 additions & 4 deletions modules/connectors/canpar/karrio/providers/canpar/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"""Karrio Canpar client settings."""

import karrio.lib as lib
from karrio.core.settings import Settings as BaseSettings
from karrio.core.utils import Envelope, apply_namespaceprefix, XP, Header

LanguageEnum = lib.units.create_enum("LanguageEnum", ["en", "fr"])


class Settings(BaseSettings):
"""Canpar connection settings."""

username: str
password: str
language: str = "en"
language: LanguageEnum = "en" # type: ignore

id: str = None
account_country_code: str = "CA"
metadata: dict = {}

@property
def carrier_name(self):
Expand All @@ -29,7 +30,9 @@ def server_url(self):

@property
def tracking_url(self):
return "https://www.canpar.com/" + self.language + "/tracking/track.htm?barcode={}"
return (
"https://www.canpar.com/" + self.language + "/tracking/track.htm?barcode={}"
)

@staticmethod
def serialize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Karrio Chronopost client settings."""

import attr
from karrio.providers.chronopost.utils import Settings as BaseSettings
from karrio.providers.chronopost import utils


@attr.s(auto_attribs=True)
class Settings(BaseSettings):
class Settings(utils.Settings):
"""Chronopost connection settings."""

account_number: str # type: ignore
password: str # type: ignore
id_emit: str = "CHRFR" # type: ignore
language: str = "en_GB"
language: utils.LanguageEnum = "en_GB" # type: ignore

id: str = None
test_mode: bool = False
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
"""Karrio Chronopost client settings."""

import karrio.lib as lib
from karrio.schemas.chronopost.shippingservice import headerValue
from karrio.core.settings import Settings as BaseSettings


LanguageEnum = lib.units.create_enum("LanguageEnum", ["en_GB", "fr_FR"])


class Settings(BaseSettings):
"""Chronopost connection settings."""

account_number: str
password: str
id_emit: str = "CHRFR"
language: str = "en_GB"
language: LanguageEnum = "en_GB" # type: ignore

account_country_code: str = "FR"
metadata: dict = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Karrio DHL Universal settings."""

import attr
from karrio.providers.dhl_universal.utils import Settings as BaseSettings
import karrio.providers.dhl_universal.utils as utils


@attr.s(auto_attribs=True)
class Settings(BaseSettings):
class Settings(utils.Settings):
"""DHL Universal connection settings."""

# Carrier specific properties
consumer_key: str
consumer_secret: str
language: str = "en"
language: utils.LanguageEnum = "en" # type: ignore

# Base properties
id: str = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import karrio.lib as lib
from karrio.core import Settings as BaseSettings

LanguageEnum = lib.units.create_enum("LanguageEnum", ["en", "de"])


class Settings(BaseSettings):
"""DHL Universal connection settings."""

# Carrier specific properties
consumer_key: str
consumer_secret: str
language: str = "en"
language: LanguageEnum = "en" # type: ignore

account_country_code: str = "DE"
metadata: dict = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Settings(provider_utils.Settings):
api_key: str
identifier: str
code_client: str = None
language: str = "fr"
language: provider_utils.LanguageEnum = "fr" # type: ignore

# generic properties
id: str = None
Expand Down
4 changes: 3 additions & 1 deletion modules/connectors/geodis/karrio/providers/geodis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import karrio.lib as lib
import karrio.core as core

LanguageEnum = lib.units.create_enum("LanguageEnum", ["fr", "en"])


class Settings(core.Settings):
"""GEODIS connection settings."""

api_key: str
identifier: str
code_client: str = None
language: str = "fr"
language: LanguageEnum = "fr" # type: ignore

account_country_code: str = "FR"
metadata: dict = {}
Expand Down
6 changes: 3 additions & 3 deletions modules/connectors/laposte/karrio/mappers/laposte/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Karrio La Poste client settings."""

import attr
import karrio.providers.laposte.utils as provider_utils
import karrio.providers.laposte.utils as utils


@attr.s(auto_attribs=True)
class Settings(provider_utils.Settings):
class Settings(utils.Settings):
"""La Poste connection settings."""

# required carrier specific properties
api_key: str
lang: str = "fr_FR"
lang: utils.LangEnum = "fr_FR" # type: ignore

# generic properties
id: str = ""
Expand Down
6 changes: 5 additions & 1 deletion modules/connectors/laposte/karrio/providers/laposte/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import karrio.lib as lib
import karrio.core as core


LangEnum = lib.units.create_enum("LangEnum", ["fr_FR", "en_US"])


class Settings(core.Settings):
"""La Poste connection settings."""

api_key: str
lang: str = "fr_FR"
lang: LangEnum = "fr_FR" # type: ignore

@property
def carrier_name(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Settings(provider_utils.Settings):
api_key: str
customer_id: str
billing_account: str = None
language: str = "en"
language: provider_utils.LanguageEnum = "en" # type: ignore

# generic properties
id: str = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import base64
import karrio.lib as lib
import karrio.core as core

LanguageEnum = lib.units.create_enum("LanguageEnum", ["en", "fr"])


class Settings(core.Settings):
"""Nationex connection settings."""

api_key: str
customer_id: str
billing_account: str = None
language: str = "en"
language: LanguageEnum = "en" # type: ignore

@property
def carrier_name(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Karrio Purolator connection settings."""

import attr
from karrio.providers.purolator.utils import Settings as BaseSettings
import karrio.providers.purolator.utils as utils


@attr.s(auto_attribs=True)
class Settings(BaseSettings):
class Settings(utils.Settings):
"""Purolator connection settings."""

username: str
password: str
account_number: str
language: str = "en"
user_token: str = None
language: utils.LanguageEnum = "en" # type: ignore

id: str = None
test_mode: bool = False
Expand Down
10 changes: 7 additions & 3 deletions modules/connectors/purolator/karrio/providers/purolator/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from base64 import b64encode
import base64

import karrio.lib as lib
from karrio.core import Settings as BaseSettings
from karrio.core.utils import Envelope, apply_namespaceprefix, XP

LanguageEnum = lib.units.create_enum("LanguageEnum", ["en", "fr"])


class Settings(BaseSettings):
"""Purolator connection settings."""

username: str
password: str
account_number: str
language: str = "en"
user_token: str = None
language: LanguageEnum = "en" # type: ignore

id: str = None
account_country_code: str = "CA"
Expand All @@ -31,7 +35,7 @@ def server_url(self):
@property
def authorization(self):
pair = "%s:%s" % (self.username, self.password)
return b64encode(pair.encode("utf-8")).decode("ascii")
return base64.b64encode(pair.encode("utf-8")).decode("ascii")

@property
def tracking_url(self):
Expand Down
9 changes: 8 additions & 1 deletion packages/ui/modals/connect-provider-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,15 @@ export const ConnectProviderModal: React.FC<ConnectProviderModalComponent> = ({
const handleCarrierChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const target = event.target;
const value = target.value as CarrierNameType;
let state = {
const state = {
carrier_id: `${value.toLocaleLowerCase()}${testMode ? "-test" : ""}`,
credentials: Object.entries(connection_fields[value] || {}).reduce(
(acc, [key, field]) => ({
...acc,
[key]: field.default,
}),
{} as any,
),
};

setCarrierName(value);
Expand Down

0 comments on commit 5a40c51

Please sign in to comment.