Skip to content

Commit

Permalink
login
Browse files Browse the repository at this point in the history
  • Loading branch information
matin committed Nov 17, 2019
1 parent 0fa98e1 commit 8caae07
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions dhlmex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ['__version__']
__all__ = ['__version__', 'Client']

from .client import Client
from .version import __version__

Empty file removed dhlmex/auth.py
Empty file.
75 changes: 36 additions & 39 deletions dhlmex/client.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import os
from typing import Any, ClassVar, Dict, Optional, Tuple, Union
from typing import Any, ClassVar, Dict, Optional

from requests import Response, Session

API_URL = 'https://api.getmati.com'
from .resources import Resource

API_URL = 'https://prepaid.dhl.com.mx/Prepago'
USER_AGENT = (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
)


class Client:

base_url: ClassVar[str] = API_URL
basic_auth_creds: Tuple[str, str]
bearer_tokens: Dict[Union[None, str], AccessToken]
headers: Dict[str, str]
session: Session

# resources
access_tokens: ClassVar = AccessToken
identities: ClassVar = Identity
user_validation_data: ClassVar = UserValidationData
verifications: ClassVar = Verification
...

def __init__(
self, api_key: Optional[str] = None, secret_key: Optional[str] = None
self, username: Optional[str] = None, password: Optional[str] = None
):
username = username or os.environ['DHLMEX_USERNAME']
password = password or os.environ['DHLMEX_PASSWORD']
self.session = Session()
self.headers = {'User-Agent': f'mati-python/{client_version}'}
api_key = api_key or os.environ['MATI_API_KEY']
secret_key = secret_key or os.environ['MATI_SECRET_KEY']
self.basic_auth_creds = (api_key, secret_key)
self.bearer_tokens = {}
self.session.headers['User-Agent'] = USER_AGENT
self._login(username, password)
Resource._client = self

def get_valid_bearer_token(
self, score: Optional[str] = None
) -> AccessToken:
try:
expired = self.bearer_tokens[score].expired
except KeyError:
expired = True
if expired: # renew token
self.bearer_tokens[score] = self.access_tokens.create(score)
return self.bearer_tokens[score]
def _login(self, username: str, password: str) -> Response:
self.get('/') # Initialize cookies
endpoint = '/jsp/app/login/login.xhtml'
data = {
'AJAXREQUEST': '_viewRoot',
'j_id6': 'j_id6',
'j_id6:j_id20': username,
'j_id6:j_id22': password,
'javax.faces.ViewState': 'j_id4',
'j_id6:j_id29': 'j_id6:j_id29',
}
return self.post(endpoint, data)

def get(self, endpoint: str, **kwargs: Any) -> Dict[str, Any]:
return self.request('get', endpoint, **kwargs)
def get(self, endpoint: str, **kwargs: Any) -> Response:
return self.request('get', endpoint, {}, **kwargs)

def post(self, endpoint: str, **kwargs: Any) -> Dict[str, Any]:
return self.request('post', endpoint, **kwargs)
def post(
self, endpoint: str, data: Dict[str, str], **kwargs: Any
) -> Response:
return self.request('post', endpoint, data, **kwargs)

def request(
self,
method: str,
endpoint: str,
auth: Union[str, AccessToken, None] = None,
token_score: Optional[str] = None,
**kwargs: Any,
) -> Dict[str, Any]:
self, method: str, endpoint: str, data: Dict[str, str], **kwargs: Any,
) -> Response:
url = self.base_url + endpoint
auth = auth or self.get_valid_bearer_token(token_score)
headers = {**self.headers, **dict(Authorization=str(auth))}
response = self.session.request(method, url, headers=headers, **kwargs)
response = self.session.request(method, url, data=data, **kwargs)
self._check_response(response)
return response.json()
return response

@staticmethod
def _check_response(response: Response) -> None:
Expand Down
3 changes: 3 additions & 0 deletions dhlmex/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__all__ = ['Resource']

from .base import Resource
5 changes: 5 additions & 0 deletions dhlmex/resources/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import ClassVar


class Resource:
_client: ClassVar['cepmex.Client'] # type: ignore

0 comments on commit 8caae07

Please sign in to comment.