The Service
Endpoint Tutorial
Note
This tutorial assumes you have already set up a Python environment on your machine. If not, see Setting up a Python Environment for help on how to set this up.
Definition
See the Service Endpoint for a definition of this endpoint.
Tutorial
This is a simple tutorial that lists the services that you have available to you.
Create a new empty folder
Create an empty text file in that folder and copy and paste the following Python code into the file and save it as a python file (e.g. as
ServiceEndpointTutorial.py
):""" Python script to call the SF-API Service endpoint on the given server. This lists the services (their names as strings) available to the user whose API token is being used. """ import requests sf_web_api_url = 'https://solarfarmer.dnv.com/latest/api' api_token = 'ENTER YOUR API TOKEN HERE!!!' service_endpoint_url = sf_web_api_url + '/service' try: headers = { 'Authorization': f'Bearer {api_token}', 'ContentType': 'application/json'} # Call the HTTP GET request to the API print(f'Calling {service_endpoint_url}') response = requests.get(service_endpoint_url, headers = headers) # raise an error if the call was not successful response.raise_for_status() if (response.ok): output_response = response.json() if ('services' in output_response): services = output_response['services'] print(f'{len(services)} service(s) returned:') for service in services: print(service) else: print('No services returned') print(f'output_response = {output_response}') else: print(f"API request {service_endpoint_url} failed.") except Exception as exc: print(f"Exception thrown when calling {service_endpoint_url}") print(str(exc))
Replace the text 'ENTER YOUR API TOKEN HERE!!!' with your personal API token (encase it in single quotes)
Open a command prompt in the folder
Type
python ServiceEndpointTutorial.py
and hit Return to run the script.It should return very quickly, giving some output similar to the following:
Calling https://solarfarmer.dnv.com/latest/api/service 2 service(s) returned: ModelChain2D ModelChain3D