From 9e7082238467798ac8253fc2e0a69e61d8a13647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Prunayre?= Date: Tue, 19 Nov 2024 17:37:47 +0100 Subject: [PATCH 1/2] Doc / Update Python example * Use `srv/api/me` endpoint (instead of old API) * Use session once connected. --- docs/manual/docs/api/the-geonetwork-api.md | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/docs/manual/docs/api/the-geonetwork-api.md b/docs/manual/docs/api/the-geonetwork-api.md index 0708f57c4bb..16a998b907b 100644 --- a/docs/manual/docs/api/the-geonetwork-api.md +++ b/docs/manual/docs/api/the-geonetwork-api.md @@ -435,32 +435,34 @@ password = 'password' # Set up your server and the authentication URL: server = "http://localhost:8080" -authenticate_url = server + '/geonetwork/srv/eng/info?type=me' -# To generate the XRSF token, send a post request to the following URL: http://localhost:8080/geonetwork/srv/eng/info?type=me +authenticate_url = server + '/geonetwork/srv/api/me' + session = requests.Session() -response = session.post(authenticate_url) +session.auth = (username, password) +response = session.get(authenticate_url) # Extract XRSF token xsrf_token = response.cookies.get("XSRF-TOKEN") if xsrf_token: - print ("The XSRF Token is:", xsrf_token) + print("The XSRF Token is:", xsrf_token) else: - print("Unable to find the XSRF token") - -# You can now use the username and password, along with the XRSF token to send requests to the API. + print("Unable to find the XSRF token") # This example will add an online resource to a specified UUID using the http://localhost:8080/geonetwork/srv/api/records/batchediting endpoint # Set header for connection -headers = {'Accept': 'application/json', -'X-XSRF-TOKEN': xsrf_token +headers = { + 'Accept': 'application/json', + 'X-XSRF-TOKEN': xsrf_token, + 'JSESSION_ID': response.cookies.get("JSESSIONID") } # Set the parameters -params = {'uuids': 'the uuid to be updated', -'bucket': 'bucketname', -'updateDateStamp': 'true', +params = { + 'uuids': 'the uuid to be updated', + 'bucket': 'bucketname', + 'updateDateStamp': 'true', } # Set the JSON data: note that the value must have one of , , or @@ -473,7 +475,6 @@ json_data = [{'condition': '', # Send a put request to the endpoint response = session.put(server + 'geonetwork/srv/api/records/batchediting', params=params, -auth = (username, password), headers=headers, json=json_data, ) From 551f1abb3a80d2036504a49128c2816e0d931af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Prunayre?= Date: Mon, 9 Dec 2024 12:01:00 +0100 Subject: [PATCH 2/2] Black formatting and fix missing / --- docs/manual/docs/api/the-geonetwork-api.md | 42 ++++++++++++---------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/docs/manual/docs/api/the-geonetwork-api.md b/docs/manual/docs/api/the-geonetwork-api.md index 16a998b907b..ea421cbe9b3 100644 --- a/docs/manual/docs/api/the-geonetwork-api.md +++ b/docs/manual/docs/api/the-geonetwork-api.md @@ -430,13 +430,13 @@ This is an example of how to use requests in python to authenticate to the API a import requests # Set up your username and password: -username = 'username' -password = 'password' +username = "username" +password = "password" # Set up your server and the authentication URL: server = "http://localhost:8080" -authenticate_url = server + '/geonetwork/srv/api/me' +authenticate_url = server + "/geonetwork/srv/api/me" session = requests.Session() session.auth = (username, password) @@ -445,38 +445,42 @@ response = session.get(authenticate_url) # Extract XRSF token xsrf_token = response.cookies.get("XSRF-TOKEN") if xsrf_token: - print("The XSRF Token is:", xsrf_token) + print("The XSRF Token is:", xsrf_token) else: - print("Unable to find the XSRF token") + print("Unable to find the XSRF token") # This example will add an online resource to a specified UUID using the http://localhost:8080/geonetwork/srv/api/records/batchediting endpoint # Set header for connection headers = { - 'Accept': 'application/json', - 'X-XSRF-TOKEN': xsrf_token, - 'JSESSION_ID': response.cookies.get("JSESSIONID") + "Accept": "application/json", + "X-XSRF-TOKEN": xsrf_token, + "JSESSION_ID": response.cookies.get("JSESSIONID"), } # Set the parameters params = { - 'uuids': 'the uuid to be updated', - 'bucket': 'bucketname', - 'updateDateStamp': 'true', + "uuids": "the uuid to be updated", + "bucket": "bucketname", + "updateDateStamp": "true", } # Set the JSON data: note that the value must have one of , , or -json_data = [{'condition': '', -'value': 'https://localhostWWW:LINK-1.0-http--linkThe Title of the URLThe description of the resource', -'xpath': '/gmd:MD_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions', -}, +json_data = [ + { + "condition": "", + "value": 'https://localhostWWW:LINK-1.0-http--linkThe Title of the URLThe description of the resource', + "xpath": "/gmd:MD_Metadata/gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions", + }, ] # Send a put request to the endpoint -response = session.put(server + 'geonetwork/srv/api/records/batchediting', -params=params, -headers=headers, -json=json_data, +response = session.put( + server + "/geonetwork/srv/api/records/batchediting", + params=params, + headers=headers, + json=json_data, ) print(response.text) + ```