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
16 changes: 1 addition & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
tox -e lint


tox-coveralls:
coveralls:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -61,26 +61,12 @@ jobs:
with:
python-version: 3.7

- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox pytest pytest-cov coverage responses
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Tox testenv
run: |
tox

- name: Pytest
run: |
pytest
Expand Down
31 changes: 8 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,13 @@ on `osx / linux / bashonwindows`:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
```

- Once you have **Poetry** installed you need to install the projects dependencies with this command:
- Once you have **Poetry** installed you need to install the projects dependencies with this command from the project root:

```bash
poetry install
bin/setup
```

- After you have installed **Poetry**, you need to start the python environment managed by Poetry by
running `poetry shell` in your terminal.

```bash
poetry shell
```
- This script will install all dependencies specified in `pyproject.toml` via `Poetry` and install the `pre-commit` hooks
this project uses.

## Adding dependencies to the project
If your changes require you to install a python package/module using `poetry add <some package>` or
Expand All @@ -100,21 +95,11 @@ poetry export -f requirements.txt --output requirements.txt --without-hashes --d
## Pre-Commit Hooks
We are using [Pre-Commit](https://pre-commit.com/) to enforce formatting, lint rules, and code analysis so that
this repo is always in good health.
- If you choose not to globally install `pre-commit`, then you can skip installing via `pip` or `homebrew` directly.
You can simply run either `pip install -r requirements.txt` or `poetry install`
To be able to push a PR to the repo after making changes locally, you will need to install `pre-commit` which
is a tool that runs linting, formatting, and code analysis on your changes.
```bash
pip install pre-commit # Install via pip

OR
- `Pre-Commit` is installed and initialized when you run `bin/setup` from the project root as outlined above.

brew install pre-commit # Install via homebrew
```
- After you have run either `pip install -r requirements.txt`, `poetry install`, or globally installed
[Pre-Commit](https://pre-commit.com/) using the above commands you need to run the following command
in the project directory locally. This allows the pre-commit hooks to run when you are looking to commit
and push code to this repository.
- If you choose not to use `Poetry` and prefer `pip` you can simply run `pip install -r requirements.txt`
To be able to commit & push a PR to the repo after making changes locally, you will need to install `pre-commit` which
is a tool that runs tests, linting, formatting, and code analysis on your changes.
```bash
pre-commit install
```
Expand Down
5 changes: 5 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

poetry install
pre-commit install
poetry shell
83 changes: 82 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ isort = "^5.8.0"
responses = "^0.13.3"
coveralls = "^3.1.0"
pre-commit = "2.13.0"
pytest-cache = "^1.0"
pytest-watch = "^4.2.0"

[tool.black]
line-length = 100
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dataclasses-json==0.5.4; python_version >= "3.6"
distlib==0.3.2; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
docopt==0.6.2; python_version >= "3.5"
docutils==0.16; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.5"
execnet==1.9.0; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
filelock==3.0.12; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
flake8==3.9.2; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
fuuid==0.1.0; python_version >= "3.7" and python_version < "4.0"
Expand Down Expand Up @@ -45,8 +46,10 @@ pyflakes==2.3.1; python_version >= "2.7" and python_full_version < "3.0.0" or py
pygments==2.9.0; python_version >= "3.5"
pyparsing==2.4.7; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.3.0" and python_version >= "3.6"
pytest==6.2.4; python_version >= "3.6"
pytest-cache==1.0
pytest-cov==2.12.1; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
pytest-mock==3.6.1; python_version >= "3.6"
pytest-watch==4.2.0
python-dotenv==0.15.0
pytz==2021.1; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.5"
pyyaml==5.4.1; python_full_version >= "3.6.1"
Expand All @@ -70,5 +73,6 @@ typing-extensions==3.10.0.0; python_version < "3.8" and python_version >= "3.6"
typing-inspect==0.7.1; python_version >= "3.6"
urllib3==1.26.6; python_version >= "3.5" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version < "4" and python_version >= "3.5"
virtualenv==20.4.7; python_full_version >= "3.6.1"
watchdog==2.1.3; python_version >= "3.6"
yarl==1.6.3; python_version >= "3.6"
zipp==3.5.0; python_version < "3.8" and python_version >= "3.6"
18 changes: 14 additions & 4 deletions shipengine_sdk/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ def __init__(self, events: Optional[List[str]] = None) -> None:
def get_subscribers(self, event: Optional[str] = None):
return self.events[event]

def register(self, event, subscriber, callback: Optional[Callable] = None):
if callback is None:
callback = getattr(subscriber, "update")
def register(self, event, subscriber, callback: Union[Callable, str] = None):
if event is Events.ON_REQUEST_SENT.value and callback is None:
callback = getattr(subscriber, "catch_request_sent_event")
elif event is Events.ON_RESPONSE_RECEIVED.value and callback is None:
callback = getattr(subscriber, "catch_response_received_event")
self.get_subscribers(event)[subscriber] = callback

def unregister(self, event, subscriber):
Expand Down Expand Up @@ -126,6 +128,14 @@ def __init__(self, name=None) -> None:
def update(event: Union[RequestSentEvent, ResponseReceivedEvent]):
return event

@staticmethod
def catch_request_sent_event(event: RequestSentEvent):
return event

@staticmethod
def catch_response_received_event(event: ResponseReceivedEvent):
return event

def to_dict(self):
return (lambda o: o.__dict__)(self)

Expand Down Expand Up @@ -163,7 +173,7 @@ def emit_event(emitted_event_type: str, event_data, dispatcher: Dispatcher):
request_id=event_data.id,
base_uri=event_data.base_uri,
status_code=event_data.status_code,
headers=event_data.request_headers,
headers=event_data.response_headers,
body=event_data.body,
retry=event_data.retry,
elapsed=event_data.elapsed,
Expand Down
31 changes: 25 additions & 6 deletions shipengine_sdk/http_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ def base_url(config) -> str:
return config.base_uri if os.getenv("CLIENT_BASE_URI") is None else os.getenv("CLIENT_BASE_URI")


def generate_event_message(retry: int, method: str, base_uri: str) -> str:
def generate_event_message(
retry: int,
method: str,
base_uri: str,
status_code: Optional[int] = None,
message_type: Optional[str] = None,
) -> str:
if message_type == "received":
if retry > 0:
f"Retrying the ShipEngine {method} API at {base_uri}"
else:
return f"Received an HTTP {status_code} response from the ShipEngine {method} API"

if retry == 0:
return ShipEngineEvent.new_event_message(
method=method, base_uri=base_uri, message_type="base_message"
Expand All @@ -44,11 +56,12 @@ def generate_event_message(retry: int, method: str, base_uri: str) -> str:
)


def request_headers(user_agent: str) -> Dict[str, Any]:
def request_headers(user_agent: str, api_key: str) -> Dict[str, Any]:
return {
"User-Agent": user_agent,
"Content-Type": "application/json",
"Accept": "application/json",
"Api-Key": api_key,
}


Expand Down Expand Up @@ -86,7 +99,7 @@ def send_rpc_request(
base_uri = base_url(config=config)

request_body: Dict[str, Any] = wrap_request(method=method, params=params)
req_headers = request_headers(self._derive_user_agent())
req_headers = request_headers(user_agent=self._derive_user_agent(), api_key=config.api_key)
req: Request = Request(
method="POST",
url=base_uri,
Expand Down Expand Up @@ -128,8 +141,15 @@ def send_rpc_request(
resp_body: Dict[str, Any] = resp.json()
status_code: int = resp.status_code

response_received_message = generate_event_message(
retry=retry,
method=method,
base_uri=base_uri,
status_code=status_code,
message_type="received",
)
response_event_data = EventOptions(
message=request_event_message,
message=response_received_message,
id=request_body["id"],
base_uri=base_uri,
status_code=status_code,
Expand Down Expand Up @@ -175,8 +195,7 @@ def _derive_user_agent() -> str:
:rtype: str
"""
sdk_version: str = f"shipengine-python/{__version__}"
os_kernel: str = platform.platform(terse=True)
python_version: str = platform.python_version()
python_implementation: str = platform.python_implementation()

return f"{sdk_version} {os_kernel} {python_version} {python_implementation}"
return f"{sdk_version} {python_implementation}-v{python_version}"
2 changes: 1 addition & 1 deletion shipengine_sdk/jsonrpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def rpc_request_loop(
and err.retry_after < config.timeout
):
time.sleep(err.retry_after)
retry += 1
continue
else:
raise err
retry += 1
return api_response
Loading