Skip to content

Commit

Permalink
added optional parameter to use sandbox urls
Browse files Browse the repository at this point in the history
When starting out, testing or developing with this library it is useful not directly send all of the request to the regular API but instead to the dedicated ORCID sandbox. This change adds an optional parameter that would actively cause the endpoint URL be switched from the regular API to the sandbox. Since the default is set to False, this change is backward compatible with existing implementations that use this library.
  • Loading branch information
DavidFichtmueller committed Feb 1, 2024
1 parent 46d9323 commit 9f23873
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/pyorcid/orcid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Orcid():
'''
This is a wrapper class for ORCID API
'''
def __init__(self,orcid_id, orcid_access_token = " ", state="public") -> None:
def __init__(self,orcid_id, orcid_access_token = " ", state="public", sandbox=False) -> None:
'''
Initialize orcid instance
orcid_id : Orcid ID of the user
Expand All @@ -17,6 +17,7 @@ def __init__(self,orcid_id, orcid_access_token = " ", state="public") -> None:
self._orcid_id = orcid_id
self._orcid_access_token = orcid_access_token
self._state = state
self._sandbox = sandbox
#For testing purposes (pytesting on github workflow)
if orcid_access_token!=" ":
try:
Expand Down Expand Up @@ -45,12 +46,14 @@ def __is_access_token_valid(self):

if self._state == "public":
# Specify the ORCID record endpoint for the desired ORCID iD
# api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
api_url = f'https://pub.orcid.org/v3.0/{self._orcid_id}'
if(self._sandbox):
api_url = f'https://pub.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing

elif self._state == "member":
# api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
api_url = f'https://api.orcid.org/v3.0/{self._orcid_id}'
if(self._sandbox):
api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing

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

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

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

elif self._state == "member":
# api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}' #for testing
api_url = f'https://api.orcid.org/v3.0/{self._orcid_id}/{section}'
if(self._sandbox):
api_url = f'https://api.sandbox.orcid.org/v3.0/{self._orcid_id}/{section}' #for testing


# Make a GET request to retrieve the ORCID record
response = requests.get(api_url, headers=headers)
Expand Down
17 changes: 12 additions & 5 deletions src/pyorcid/orcid_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ class OrcidAuthentication:
The Orcid's OAuth 2.0 authorrization is used to access the ORCID record of the user that gave access.
'''
def __init__(self, client_id, client_secret, redirect_uri=""):
def __init__(self, client_id, client_secret, redirect_uri="", sandbox=False):
'''
initializes the ORCidAuthentication and gets the access token
Parameters
----------
client_id : str : client id obtained from the registered application
client_secret : str : client secret obtained from the registered application
redirect_uri : str : redirect uri obtained from the registered application
sandbox : str : a boolean value to show if the ORCID sandbox API should be used (default: False)
'''
self.__client_id = client_id
self.__client_secret = client_secret
self.__redirect_uri = redirect_uri
self.__sandbox = sandbox
return None


Expand All @@ -30,13 +32,14 @@ def get_private_access_token(self):
Requires user authorization
'''

# Set the necessary parameters
# auth_url_endpoint = "https://sandbox.orcid.org/oauth/authorize" #for testing
# token_url = "https://sandbox.orcid.org/oauth/token" #for testing

# Set the necessary parameters
auth_url_endpoint = "https://orcid.org/oauth/authorize"
token_url = "https://orcid.org/oauth/token"

if(self.__sandbox):
auth_url_endpoint = "https://sandbox.orcid.org/oauth/authorize"
token_url = "https://sandbox.orcid.org/oauth/token"

# Step 1: Redirect the user to the authorization URL
params = {
'client_id': self.__client_id,
Expand Down Expand Up @@ -73,6 +76,10 @@ def get_public_access_token(self):
"""
scope='/read-public'
token_url = "https://orcid.org/oauth/token"

if(self.__sandbox):
token_url = "https://sandbox.orcid.org/oauth/token"

params = {
'client_id': self.__client_id,
'client_secret': self.__client_secret,
Expand Down

0 comments on commit 9f23873

Please sign in to comment.