Skip to content

Commit e92a4b6

Browse files
committed
Release 0.3.23b0
1 parent c9f86c7 commit e92a4b6

File tree

8 files changed

+133
-30
lines changed

8 files changed

+133
-30
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ or in your requirements.txt
1616
# Find the version you want to pin:
1717
# https://pypi.org/project/embloy-sdk/#history
1818
# Specify that version in your requirements.txt file
19-
embloy_sdk>=0.3.1b
19+
embloy_sdk>=0.3.23b
2020
```
2121

2222
Integrate it in your service:
@@ -33,8 +33,14 @@ session = EmbloySession("job", "your_job_slug", "your_success_url", "your_cancel
3333
redirect_url = EmbloyClient(client_token, session).make_request()
3434
```
3535

36+
## Run the tests
37+
```Bash
38+
python -m unittest tests/test_embloy_client.py
39+
```
40+
3641
## Publish Package
3742
```Bash
43+
3844
python setup.py sdist bdist_wheel
3945

4046
twine upload dist/*

embloy_sdk/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .embloy_client import EmbloyClient
1+
from .embloy_client import EmbloyClient
2+
from .embloy_session import EmbloySession, SessionOptions

embloy_sdk/embloy_client.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import requests
2+
from .embloy_session import EmbloySession
3+
import json
24

35
class EmbloyClient:
46
"""
@@ -26,57 +28,65 @@ class EmbloyClient:
2628
"""
2729
def __init__(self, client_token, session, api_url='https://api.embloy.com', base_url='https://embloy.com', api_version='api/v0'):
2830
if not isinstance(client_token, str):
29-
raise ValueError('clientToken must be a string')
31+
raise ValueError('client_token must be a string')
32+
if not isinstance(session, EmbloySession):
33+
raise ValueError('session must be an instance of EmbloySession')
3034
self.client_token = client_token
3135
self.session = session
3236
self.api_url = api_url
3337
self.base_url = base_url
3438
self.api_version = api_version
3539

36-
def get_form_data_and_headers(self):
37-
"""
38-
Returns the form data and headers for a request.
39-
40-
Returns
41-
-------
42-
tuple
43-
A tuple containing the form data and headers.
44-
"""
40+
def form_request(self):
4541
data = {
4642
'mode': self.session.mode,
4743
'job_slug': self.session.job_slug,
48-
'success_url': self.session.success_url,
49-
'cancel_url': self.session.cancel_url,
5044
}
5145

46+
if self.session.success_url is not None:
47+
data['success_url'] = self.session.success_url
48+
49+
if self.session.cancel_url is not None:
50+
data['cancel_url'] = self.session.cancel_url
51+
5252
headers = {
5353
'client_token': self.client_token,
54-
'User-Agent': 'embloy-sdk/0.3.1b (Python; Server)',
5554
'Content-Type': 'application/json',
55+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0',
5656
}
5757

5858
return data, headers
5959

60-
def make_request(self):
61-
"""
62-
Makes a request to the API and returns a URL with the request token.
60+
"""
61+
Makes a request to the API and returns a URL with the request token.
62+
63+
Returns
64+
-------
65+
str
66+
A URL with the request token for the application session.
6367
64-
Returns
65-
-------
66-
str
67-
A URL with the request token for the application session.
68+
Raises
69+
------
70+
requests.exceptions.RequestException
71+
If the request fails for any reason.
72+
"""
73+
def make_request(self):
74+
data, headers = self.form_request()
6875

69-
Raises
70-
------
71-
requests.exceptions.RequestException
72-
If the request fails for any reason.
73-
"""
74-
data, headers = self.get_form_data_and_headers()
76+
url = f"{self.api_url}/{self.api_version}/sdk/request/auth/token"
7577

7678
try:
77-
response = requests.post(f'{self.api_url}/{self.api_version}/sdk/request/auth/token', json=data, headers=headers)
79+
response = requests.request("POST", url, headers=headers, data=json.dumps(data))
80+
7881
response.raise_for_status()
7982
request_token = response.json()['request_token']
8083
return f"{self.base_url}/sdk/apply?request_token={request_token}"
8184
except requests.exceptions.RequestException as e:
85+
debug_info = {
86+
'client_token': self.client_token,
87+
'error': str(e),
88+
'request_headers': headers,
89+
'response_headers': dict(response.headers),
90+
}
91+
print('Debug Info:', debug_info)
8292
raise e

embloy_sdk/embloy_session.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class EmbloySession:
2+
"""
3+
A class used to represent a session for the Embloy API.
4+
5+
Attributes
6+
----------
7+
mode : str
8+
The mode of the session.
9+
job_slug : str
10+
The job slug for the session.
11+
success_url : str
12+
The success URL for the session.
13+
cancel_url : str
14+
The cancel URL for the session.
15+
16+
Methods
17+
-------
18+
__init__(mode, job_slug, success_url, cancel_url)
19+
Initializes the EmbloySession.
20+
"""
21+
22+
class SessionOptions:
23+
def __init__(self, success_url=None, cancel_url=None, **kwargs):
24+
self.success_url = success_url
25+
self.cancel_url = cancel_url
26+
self.__dict__.update(kwargs)
27+
28+
class EmbloySession:
29+
def __init__(self, mode, job_slug, options=SessionOptions()):
30+
"""
31+
Initializes the EmbloySession.
32+
33+
Parameters
34+
----------
35+
mode : str
36+
The mode of the session.
37+
job_slug : str
38+
The job slug for the session.
39+
success_url : str, optional
40+
The success URL for the session.
41+
cancel_url : str, optional
42+
The cancel URL for the session.
43+
"""
44+
if not mode:
45+
raise ValueError("mode is required")
46+
if not job_slug:
47+
raise ValueError("job_slug is required")
48+
49+
self.mode = mode
50+
self.job_slug = job_slug
51+
self.success_url = options.success_url
52+
self.cancel_url = options.cancel_url

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='embloy_sdk',
6-
version='0.3.1b',
6+
version='0.3.23b',
77
packages=find_packages(),
88
install_requires=[
99
'requests',

tests/__init__.py

Whitespace-only changes.

tests/test_embloy_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
import json
3+
from unittest.mock import patch, Mock
4+
from embloy_sdk import EmbloyClient, EmbloySession
5+
6+
class TestEmbloyClient(unittest.TestCase):
7+
@patch('requests.request')
8+
def test_make_request(self, mock_request):
9+
mock_response = Mock()
10+
mock_response.raise_for_status.return_value = None
11+
mock_response.json.return_value = {'request_token': 'test_token'}
12+
mock_request.return_value = mock_response
13+
14+
session = EmbloySession('test_mode', 'test_job_slug')
15+
client = EmbloyClient('test_token', session)
16+
17+
result = client.make_request()
18+
19+
self.assertEqual(result, 'https://embloy.com/sdk/apply?request_token=test_token')
20+
mock_request.assert_called_once_with(
21+
"POST",
22+
'https://api.embloy.com/api/v0/sdk/request/auth/token',
23+
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0', 'client_token': 'test_token', 'Content-Type': 'application/json'},
24+
data=json.dumps({'mode': 'test_mode', 'job_slug': 'test_job_slug'})
25+
)
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)