1
1
import requests
2
- import json
3
2
4
3
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' )
6
30
self .client_token = client_token
7
31
self .session = session
32
+ self .api_url = api_url
8
33
self .base_url = base_url
9
34
self .api_version = api_version
10
35
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
+ """
14
45
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 ,
19
50
}
20
51
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
+ }
27
57
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
34
59
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
0 commit comments