Skip to content

Services1 #274

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

Merged
merged 4 commits into from
May 15, 2023
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: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ clean:

## coverage - Test the project and generate an HTML coverage report
coverage:
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=86
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=80

## docs - Generates docs for the library
docs:
Expand Down
10 changes: 10 additions & 0 deletions easypost/easypost_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
TIMEOUT,
)
from easypost.services.address_service import AddressService
from easypost.services.batch_service import BatchService
from easypost.services.billing_service import BillingService
from easypost.services.carrier_account_service import CarrierAccountService
from easypost.services.customs_info_service import CustomsInfoService
from easypost.services.customs_item_service import CustomsItemService


class EasyPostClient:
Expand All @@ -16,3 +21,8 @@ def __init__(self, api_key: str, api_base: str = API_BASE, timeout: int = TIMEOU

# Services
self.address = AddressService(self)
self.batch = BatchService(self)
self.billing = BillingService(self)
self.carrier_account = CarrierAccountService(self)
self.customs_info = CustomsInfoService(self)
self.customs_item = CustomsItemService(self)
1 change: 1 addition & 0 deletions easypost/easypost_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import easypost


# TODO: Use the actual models here as the classes
EASYPOST_OBJECT_ID_PREFIX_TO_CLASS_NAME_MAP = {
"adr": "Address",
"ak": "ApiKey",
Expand Down
5 changes: 4 additions & 1 deletion easypost/models/address.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Address:
from easypost.easypost_object import EasyPostObject


class Address(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class ApiKey(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class Batch(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/billing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class Billing(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/brand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class Brand(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/carrier_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class CarrierAccount(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/customs_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class CustomsInfo(EasyPostObject):
pass
5 changes: 5 additions & 0 deletions easypost/models/customs_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from easypost.easypost_object import EasyPostObject


class CustomsItem(EasyPostObject):
pass
28 changes: 24 additions & 4 deletions easypost/services/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def _snakecase_name(self, class_name) -> str:

def _class_url(self, class_name) -> str:
"""Generate a URL based on class name."""
lower_class_name = class_name.lower()
if lower_class_name[-1:] in ("s", "h"):
return f"/{lower_class_name}es"
transformed_class_name = self._snakecase_name(class_name)
if transformed_class_name[-1:] in ("s", "h"):
return f"/{transformed_class_name}es"
else:
return f"/{lower_class_name}s"
return f"/{transformed_class_name}s"

def _instance_url(self, class_name, id) -> str:
"""Generate an instance URL based on the ID of the object."""
Expand Down Expand Up @@ -70,6 +70,26 @@ def _retrieve_resource(self, class_name: str, id: str) -> Any:
# TODO: Get rid of the api_key
return convert_to_easypost_object(response=response, api_key=api_key)

def _update_resource(self, class_name: str, id: str, method: RequestMethod = RequestMethod.PATCH, **params) -> Any:
"""Update an EasyPost object."""
url = self._instance_url(class_name, id)
wrapped_params = {self._snakecase_name(class_name): params}

# TODO: Don't instantiate the Requestor class, pass the client directly to the request
response, api_key = Requestor(self._client).request(method=method, url=url, params=wrapped_params)

# TODO: Get rid of the api_key
return convert_to_easypost_object(response=response, api_key=api_key)

def _delete_resource(self, class_name: str, id: str) -> Any:
"""Delete an EasyPost object."""
url = self._instance_url(class_name, id)
# TODO: Don't instantiate the Requestor class, pass the client directly to the request
response, api_key = Requestor(self._client).request(method=RequestMethod.DELETE, url=url)

# TODO: Get rid of the api_key
return convert_to_easypost_object(response=response, api_key=api_key)

def _get_next_page_resources(
self,
class_name: str,
Expand Down
79 changes: 79 additions & 0 deletions easypost/services/batch_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from typing import (
Any,
List,
)

from easypost.easypost_object import convert_to_easypost_object
from easypost.models.batch import Batch
from easypost.requestor import (
RequestMethod,
Requestor,
)
from easypost.services.base_service import BaseService


class BatchService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = Batch.__name__

def create(self, **params) -> List[Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def create(self, **params) -> List[Any]:
def create(self, **params) -> Batch:

"""Create a Batch."""
return self._create_resource(self._model_class, **params)

def all(self, **params) -> List[Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def all(self, **params) -> List[Any]:
def all(self, **params) -> List[Batch]:

"""Retrieve a list of Batches."""
return self._all_resources(self._model_class, **params)

def retrieve(self, id) -> Batch:
"""Retrieve an Batch."""
return self._retrieve_resource(self._model_class, id)

def create_and_buy(self, **params) -> Batch:
"""Create and buy a Batch in a single call."""
url = f"{self._class_url(self._model_class)}/create_and_buy"
wrapped_params = {self._snakecase_name(self._model_class): params}

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params)

return convert_to_easypost_object(response=response, api_key=api_key)

def buy(self, id: str, **params) -> Batch:
"""Buy a Batch."""
url = f"{self._instance_url(self._model_class, id)}/buy"

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)

return convert_to_easypost_object(response=response, api_key=api_key)

def label(self, id: str, **params) -> Batch:
"""Create a Batch label."""
url = f"{self._instance_url(self._model_class, id)}/label"

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)

return convert_to_easypost_object(response=response, api_key=api_key)

def remove_shipments(self, id: str, **params) -> Batch:
"""Remove Shipments from a Batch."""
url = f"{self._instance_url(self._model_class, id)}/remove_shipments"

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)

return convert_to_easypost_object(response=response, api_key=api_key)

def add_shipments(self, id: str, **params) -> Batch:
"""Add Shipments to a Batch."""
url = f"{self._instance_url(self._model_class, id)}/add_shipments"

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)

return convert_to_easypost_object(response=response, api_key=api_key)

def create_scan_form(self, id: str, **params) -> Batch:
"""Create a ScanForm for a Batch."""
url = f"{self._instance_url(self._model_class, id)}/scan_form"

response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=params)

return convert_to_easypost_object(response=response, api_key=api_key)
75 changes: 75 additions & 0 deletions easypost/services/billing_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import (
Any,
Dict,
List,
)

from easypost.easypost_object import convert_to_easypost_object
from easypost.error import Error
from easypost.models.billing import Billing
from easypost.requestor import (
RequestMethod,
Requestor,
)
from easypost.services.base_service import BaseService


class BillingService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = Billing.__name__

def fund_wallet(self, amount: str, priority: str = "primary") -> None:
"""Fund your EasyPost wallet by charging your primary or secondary payment method on file."""
endpoint, payment_method_id = self._get_payment_method_info(priority=priority)

url = f"{endpoint}/{payment_method_id}/charges"
wrapped_params = {"amount": amount}

Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params)

def delete_payment_method(self, priority: str) -> None:
"""Delete a payment method."""
endpoint, payment_method_id = self._get_payment_method_info(priority=priority)

url = f"{endpoint}/{payment_method_id}"

Requestor(self._client).request(method=RequestMethod.DELETE, url=url)

def retrieve_payment_methods(self, **params) -> Dict[str, Any]:
"""Retrieve payment methods."""
response, api_key = Requestor(self._client).request(
method=RequestMethod.GET,
url="/payment_methods",
params=params,
)

if response.get("id") is None:
raise Error(message="Billing has not been setup for this user. Please add a payment method.")

return convert_to_easypost_object(response=response, api_key=api_key)

def _get_payment_method_info(self, priority: str = "primary") -> List[str]:
"""Get payment method info (type of the payment method and ID of the payment method)"""
payment_methods = self.retrieve_payment_methods()

payment_method_map = {
"primary": "primary_payment_method",
"secondary": "secondary_payment_method",
}

payment_method_to_use = payment_method_map.get(priority)
error_string = "The chosen payment method is not valid. Please try again."

if payment_method_to_use and payment_methods[payment_method_to_use]:
payment_method_id = payment_methods[payment_method_to_use]["id"]
if payment_method_id.startswith("card_"):
endpoint = "/credit_cards"
elif payment_method_id.startswith("bank_"):
endpoint = "/bank_accounts"
else:
raise Error(message=error_string)
else:
raise Error(message=error_string)

return [endpoint, payment_method_id]
62 changes: 62 additions & 0 deletions easypost/services/carrier_account_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import (
Any,
List,
Optional,
)

from easypost.constant import _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS
from easypost.easypost_object import convert_to_easypost_object
from easypost.models.carrier_account import CarrierAccount
from easypost.requestor import (
RequestMethod,
Requestor,
)
from easypost.services.base_service import BaseService


class CarrierAccountService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = CarrierAccount.__name__

def create(self, **params) -> CarrierAccount:
"""Creates a CarrierAccount."""
carrier_account_type = params.get("type")

if carrier_account_type is None:
raise ValueError("Missing required parameter 'type'")

url = self._select_carrier_account_creation_endpoint(carrier_account_type=carrier_account_type)
wrapped_params = {self._snakecase_name(self._model_class): params}
response, api_key = Requestor(self._client).request(method=RequestMethod.POST, url=url, params=wrapped_params)

return convert_to_easypost_object(response=response, api_key=api_key)

def all(self, **params) -> List[Any]:
"""Retrieve a list of CarrierAccounts."""
return self._all_resources(self._model_class, **params)

def retrieve(self, id) -> CarrierAccount:
"""Retrieve a CarrierAccount."""
return self._retrieve_resource(self._model_class, id)

def update(self, id, **params) -> CarrierAccount:
"""Update a CarrierAccount."""
return self._update_resource(self._model_class, id, **params)

def delete(self, id):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def delete(self, id):
def delete(self, id) -> None:

"""Delete a CarrierAccount."""
self._delete_resource(self._model_class, id)

def types(self) -> List[str]:
"""Get the types of CarrierAccounts available to the User."""
response, api_key = Requestor(self._client).request(method=RequestMethod.GET, url="/carrier_types")

return convert_to_easypost_object(response=response, api_key=api_key)

def _select_carrier_account_creation_endpoint(self, carrier_account_type: Optional[Any]) -> str:
"""Determines which API endpoint to use for the creation call."""
if carrier_account_type in _CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS:
return "/carrier_accounts/register"

return "/carrier_accounts"
16 changes: 16 additions & 0 deletions easypost/services/customs_info_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from easypost.models.customs_info import CustomsInfo
from easypost.services.base_service import BaseService


class CustomsInfoService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = CustomsInfo.__name__

def create(self, **params) -> CustomsInfo:
"""Create a CustomsInfo."""
return self._create_resource(self._model_class, **params)

def retrieve(self, id) -> CustomsInfo:
"""Retrieve a CustomsInfo."""
return self._retrieve_resource(self._model_class, id)
16 changes: 16 additions & 0 deletions easypost/services/customs_item_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from easypost.models.customs_item import CustomsItem
from easypost.services.base_service import BaseService


class CustomsItemService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = CustomsItem.__name__

def create(self, **params) -> CustomsItem:
"""Create a CustomsItem."""
return self._create_resource(self._model_class, **params)

def retrieve(self, id) -> CustomsItem:
"""Retrieve a CustomsItem."""
return self._retrieve_resource(self._model_class, id)
Loading