Skip to content

Commit c9f86c7

Browse files
committed
Release 0.3.1b0
1 parent 23f4f91 commit c9f86c7

File tree

3 files changed

+77
-37
lines changed

3 files changed

+77
-37
lines changed

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Embloy Node
1+
# Embloy Python
22

3-
Embloy's Node SDK for interacting with your Embloy integration.
3+
Embloy's Python SDK for interacting with your Embloy integration.
44

55
## Usage
66

@@ -16,29 +16,21 @@ 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>=0.2.0
19+
embloy_sdk>=0.3.1b
2020
```
2121

2222
Integrate it in your service:
2323

2424
```Python
25-
// In your application or script
26-
from embloy_sdk import EmbloyClient
25+
# In your application or script
26+
from embloy_sdk import EmbloyClient, EmbloySession
2727

2828
# Replace with your actual values
2929
client_token = 'your_client_token'
30-
session = {
31-
'mode': 'job',
32-
'success_url': 'your_success_url',
33-
'cancel_url': 'your_cancel_url',
34-
'job_slug': 'your_job_slug'
35-
}
36-
37-
# Create an instance of the EmbloyClient
38-
embloy_client = EmbloyClient(client_token, session)
30+
session = EmbloySession("job", "your_job_slug", "your_success_url", "your_cancel_url")
3931

4032
# Make a request to the Embloy API
41-
redirect_url = embloy_client.make_request()
33+
redirect_url = EmbloyClient(client_token, session).make_request()
4234
```
4335

4436
## Publish Package

embloy_sdk/embloy_client.py

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
11
import requests
2-
import json
32

43
class EmbloyClient:
5-
def __init__(self, client_token, session, base_url='https://api.embloy.com', api_version='api/v0'):
4+
"""
5+
A class used to represent a client for the Embloy API.
6+
7+
Attributes
8+
----------
9+
client_token : str
10+
The client token for the API.
11+
session : EmbloySession
12+
The session associated with the client.
13+
api_url : str
14+
The URL for the API.
15+
base_url : str
16+
The base URL for the API.
17+
api_version : str
18+
The version of the API.
19+
20+
Methods
21+
-------
22+
get_form_data_and_headers()
23+
Returns the form data and headers for a request.
24+
make_request()
25+
Makes a request to the API and returns a URL with the request token.
26+
"""
27+
def __init__(self, client_token, session, api_url='https://api.embloy.com', base_url='https://embloy.com', api_version='api/v0'):
28+
if not isinstance(client_token, str):
29+
raise ValueError('clientToken must be a string')
630
self.client_token = client_token
731
self.session = session
32+
self.api_url = api_url
833
self.base_url = base_url
934
self.api_version = api_version
1035

11-
def make_request(self):
12-
url = f'{self.base_url}/{self.api_version}/sdk/request/auth/token'
13-
headers = {'client_token': self.client_token}
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+
"""
1445
data = {
15-
'mode': self.session.get('mode', 'job'),
16-
'success_url': self.session.get('success_url', ''),
17-
'cancel_url': self.session.get('cancel_url', ''),
18-
'job_slug': self.session.get('job_slug', '')
46+
'mode': self.session.mode,
47+
'job_slug': self.session.job_slug,
48+
'success_url': self.session.success_url,
49+
'cancel_url': self.session.cancel_url,
1950
}
2051

21-
try:
22-
response = requests.post(url, headers=headers, data=data)
23-
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
24-
return self.handle_response(response)
25-
except requests.exceptions.RequestException as e:
26-
raise Exception(f"Error making request: {str(e)}")
52+
headers = {
53+
'client_token': self.client_token,
54+
'User-Agent': 'embloy-sdk/0.3.1b (Python; Server)',
55+
'Content-Type': 'application/json',
56+
}
2757

28-
def handle_response(self, response):
29-
if response.status_code == 200:
30-
request_token = json.loads(response.text)['request_token']
31-
return f"@{self.base_url}/sdk/apply?token={request_token}"
32-
else:
33-
raise Exception(f"Error making request: {response.text}")
58+
return data, headers
3459

60+
def make_request(self):
61+
"""
62+
Makes a request to the API and returns a URL with the request token.
63+
64+
Returns
65+
-------
66+
str
67+
A URL with the request token for the application session.
68+
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()
75+
76+
try:
77+
response = requests.post(f'{self.api_url}/{self.api_version}/sdk/request/auth/token', json=data, headers=headers)
78+
response.raise_for_status()
79+
request_token = response.json()['request_token']
80+
return f"{self.base_url}/sdk/apply?request_token={request_token}"
81+
except requests.exceptions.RequestException as e:
82+
raise e

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.2.0',
6+
version='0.3.1b',
77
packages=find_packages(),
88
install_requires=[
99
'requests',

0 commit comments

Comments
 (0)