Skip to content

Commit 8404eb2

Browse files
authored
Merge pull request #5 from DavidFichtmueller/main
added optional parameter to use sandbox urls
2 parents 46d9323 + 9f23873 commit 8404eb2

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/pyorcid/orcid.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Orcid():
77
'''
88
This is a wrapper class for ORCID API
99
'''
10-
def __init__(self,orcid_id, orcid_access_token = " ", state="public") -> None:
10+
def __init__(self,orcid_id, orcid_access_token = " ", state="public", sandbox=False) -> None:
1111
'''
1212
Initialize orcid instance
1313
orcid_id : Orcid ID of the user
@@ -17,6 +17,7 @@ def __init__(self,orcid_id, orcid_access_token = " ", state="public") -> None:
1717
self._orcid_id = orcid_id
1818
self._orcid_access_token = orcid_access_token
1919
self._state = state
20+
self._sandbox = sandbox
2021
#For testing purposes (pytesting on github workflow)
2122
if orcid_access_token!=" ":
2223
try:
@@ -45,12 +46,14 @@ def __is_access_token_valid(self):
4546

4647
if self._state == "public":
4748
# Specify the ORCID record endpoint for the desired ORCID iD
48-
# api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
4949
api_url = f'https://pub.orcid.org/v3.0/{self._orcid_id}'
50+
if(self._sandbox):
51+
api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
5052

5153
elif self._state == "member":
52-
# api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
5354
api_url = f'https://api.orcid.org/v3.0/{self._orcid_id}'
55+
if(self._sandbox):
56+
api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
5457

5558
response = requests.get(api_url, headers=headers)
5659

@@ -79,12 +82,15 @@ def __read_section(self,section="record"):
7982

8083
if self._state == "public":
8184
# Specify the ORCID record endpoint for the desired ORCID iD
82-
# api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
8385
api_url = f'https://pub.orcid.org/v3.0/{self._orcid_id}/{section}'
86+
if(self._sandbox):
87+
api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}/{section}' #for testing
8488

8589
elif self._state == "member":
86-
# api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
8790
api_url = f'https://api.orcid.org/v3.0/{self._orcid_id}/{section}'
91+
if(self._sandbox):
92+
api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}/{section}' #for testing
93+
8894

8995
# Make a GET request to retrieve the ORCID record
9096
response = requests.get(api_url, headers=headers)

src/pyorcid/orcid_authentication.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ class OrcidAuthentication:
77
The Orcid's OAuth 2.0 authorrization is used to access the ORCID record of the user that gave access.
88
99
'''
10-
def __init__(self, client_id, client_secret, redirect_uri=""):
10+
def __init__(self, client_id, client_secret, redirect_uri="", sandbox=False):
1111
'''
1212
initializes the ORCidAuthentication and gets the access token
1313
Parameters
1414
----------
1515
client_id : str : client id obtained from the registered application
1616
client_secret : str : client secret obtained from the registered application
1717
redirect_uri : str : redirect uri obtained from the registered application
18+
sandbox : str : a boolean value to show if the ORCID sandbox API should be used (default: False)
1819
1920
'''
2021
self.__client_id = client_id
2122
self.__client_secret = client_secret
2223
self.__redirect_uri = redirect_uri
24+
self.__sandbox = sandbox
2325
return None
2426

2527

@@ -30,13 +32,14 @@ def get_private_access_token(self):
3032
Requires user authorization
3133
'''
3234

33-
# Set the necessary parameters
34-
# auth_url_endpoint = "https://sandbox.orcid.org/oauth/authorize" #for testing
35-
# token_url = "https://sandbox.orcid.org/oauth/token" #for testing
36-
35+
# Set the necessary parameters
3736
auth_url_endpoint = "https://orcid.org/oauth/authorize"
3837
token_url = "https://orcid.org/oauth/token"
3938

39+
if(self.__sandbox):
40+
auth_url_endpoint = "https://sandbox.orcid.org/oauth/authorize"
41+
token_url = "https://sandbox.orcid.org/oauth/token"
42+
4043
# Step 1: Redirect the user to the authorization URL
4144
params = {
4245
'client_id': self.__client_id,
@@ -73,6 +76,10 @@ def get_public_access_token(self):
7376
"""
7477
scope='/read-public'
7578
token_url = "https://orcid.org/oauth/token"
79+
80+
if(self.__sandbox):
81+
token_url = "https://sandbox.orcid.org/oauth/token"
82+
7683
params = {
7784
'client_id': self.__client_id,
7885
'client_secret': self.__client_secret,

0 commit comments

Comments
 (0)