Skip to content

Commit

Permalink
Merge pull request #5 from DavidFichtmueller/main
Browse files Browse the repository at this point in the history
added optional parameter to use sandbox urls
  • Loading branch information
sri0606 authored May 9, 2024
2 parents 46d9323 + 9f23873 commit 8404eb2
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 8404eb2

Please sign in to comment.