Skip to content

Commit

Permalink
Merge 2feb334 into 0c2d6b8
Browse files Browse the repository at this point in the history
  • Loading branch information
agarrido19 committed Jan 27, 2020
2 parents 0c2d6b8 + 2feb334 commit fbe008a
Show file tree
Hide file tree
Showing 7 changed files with 4,990 additions and 27 deletions.
33 changes: 11 additions & 22 deletions dhlmex/resources/guides.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re
from time import sleep
from typing import Dict, Tuple
from typing import Dict, Optional, Tuple

from bs4 import BeautifulSoup
from requests import HTTPError, Response
Expand All @@ -17,8 +16,8 @@
class Guide(Resource):
@classmethod
def create_guide(
cls, origin: Origin, destination: Destination, details: OrderDetails
) -> Tuple[str, str]:
cls, origin: Origin, destination: Destination, details: OrderDetails,
) -> Tuple[str, Optional[bytes]]:
guide = cls()
try:
guides_data = guide._get_guide_data()
Expand All @@ -27,13 +26,10 @@ def create_guide(
view_state = guide._fill_guide_table(
origin, destination, details
)
resp = guide._confirm_capture(view_state)
if resp.ok:
guide_number = guide._force_percent(view_state)
guide_path = guide._download_pdf(guide_number)
return guide_number, guide_path
else:
raise DhlmexException('Error while creating guide')
guide._confirm_capture(view_state)
guide_number = guide._force_percent(view_state)
guide_bytes = guide._download_pdf(guide_number)
return guide_number, guide_bytes
else:
raise DhlmexException('No available guides')
except HTTPError as httpe:
Expand Down Expand Up @@ -181,7 +177,7 @@ def _move_page(self, view_state: str, page: str) -> Response:
}
return self._client.post(self._urls['print'], final_data)

def _download_pdf(self, guide_number: str) -> str:
def _download_pdf(self, guide_number: str) -> Optional[bytes]:
resp = self._client.post(self._urls['home'], {})
data = self.get_data(resp, self._actions['download'])
resp = self._client.post(self._urls['home'], data)
Expand Down Expand Up @@ -217,14 +213,7 @@ def _download_pdf(self, guide_number: str) -> str:
}
self._client.post(self._urls['print'], guide_data)
resp = self._client.get(self._urls['pdf'])
path = ''
if resp.ok:
path = os.getenv('DOWNLOADS_DIRECTORY') or './'
path += f'/{guide_number}.pdf'
try:
with open(path, 'wb') as f:
f.write(resp.content)
return path
except OSError as ose:
raise DhlmexException(f'Error downloading guide: {str(ose)}')
return path
return resp.content
else:
return None
2 changes: 1 addition & 1 deletion dhlmex/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.3' # pragma: no cover
__version__ = '0.0.4' # pragma: no cover
2 changes: 1 addition & 1 deletion env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copy this to your .env file

DHLMEX_USERNAME=alan@cuenca.com
DHLMEX_USERNAME=username
DHLMEX_PASSWORD=password
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ def invalid_postal_code() -> Destination:
)


@pytest.fixture
def fake_destination() -> Destination:
return Destination(
company='GUADALUPE IVONNE SANTILLANES SANTILLANES',
contact='GUADALUPE IVONNE SANTILLANES SANTILLANES',
email='ivonnesantillanes0@gmail.com',
phone='6623262213',
address1='AVENIDA ARCELIA MORAGA 171',
postal_code='83105',
neighborhood='CARIDAD',
city='HERMOSILLO',
state='SON',
)


@pytest.fixture
def details() -> OrderDetails:
return OrderDetails(
Expand Down

0 comments on commit fbe008a

Please sign in to comment.