Skip to content
Dariusz Jarosz edited this page Apr 7, 2022 · 10 revisions

Installation

The traveler API can by found on pypi repository.. It can be installed using pip with the following command.

pip install Traveler-API

Getting API Key

Users can now request API key from the profile page on the traveler web application. This can be done by clicking on your name in the upper right corner and clicking profile in the drop down. Once on the profile page you should see a button that says "Generate API Key". If the button is not there contact your administrator to be given the API permission.

Initializing the API

Now that you have the API Key you can initialize the API and start querying the traveler system. The API Key will allow most read only calls and only some write commands. The other commands are reserved for higher level system accounts.

from traveler_api_factory import TravelerApiFactory

traveler_host = 'https://traveler.aps.anl.gov'
traveler_user = 'user'
traveler_api_key = '1234ApiKey'

factory = TravelerApiFactory(traveler_host, traveler_user, traveler_api_key)

Updating the traveler data using the API

This is one write calls that is possible to do using the API Key. It will allow updating traveler entries using the API. This feature allows automation of entering data however with great power comes great responsibility. The submission of travelers will still be a manual process, please review the traveler when submitting it to ensure that appropriate data was entered into the system.

See example below.

from travelerApi.models.traveler_data_options import TravelerDataOptions

traveler_api = factory.get_traveler_api()

traveler_id = '624dfcb76dfbee55357d3197'
traveler = traveler_api.get_traveler_by_id(traveler_id)

# See next section for getting input names instructions. Mapper will be some sample code with values 

# The traveler can have various input types, see examples below with some sample data. 
mapper = {
    # Checkbox 
    'd13e0b98' : True,
    # Radio Button with Valid Value
    '67ef5695' : 'radio_text_1',
    # Number Input
    'fdab9c01' : 123,
    # Date Time Input
    'bf332394': '2020-12-31T13:32',
    # Date Input
    '94c548b6': '2022-04-06',
    # Time Input
    '00648737': '17:43',
    # URL Input
    '4b027304': 'https://anl.gov',
    # Text Input
    '9dd6d60f': 'Some Text',
    # Phone number Input
    '192bc68c': '630-152-5848',
    # Email Input
    'b85a6fd7': '[email protected]'    
}

for input_name in mapper.keys():
    input_value = mapper[input_name]
    dataOpts = TravelerDataOptions(name=input_name, value=input_value)
    result = traveler_api.update_traveler_data(traveler_id, dataOpts)

Getting the input names

Here are some ways I recommend fetching the names of the input in the traveler.

  1. (Recommended) By fetching it out of the User defined key. However will not always work if the user defined keys were not specified during form design.
mapping = traveler.mapping
udk_info = "Input Name: %s \nUser Defined Key:%s"
for udk in mapping.keys():
    print(udk_info % (mapping[udk], udk))
  1. By fetching it out of the labels. This can sometimes be helpful if the labels are meaningful enough.
labels = traveler.labels

label_info = "Input Name: %s \nLabel:%s"
for label in labels.keys():
    print(label_info % (label, labels[label]))
  1. By using the web browser developer tools inspect tool. The name is the attribute the is needed for the script to work. See examples below.
<input type="checkbox" data-userkey="" name="d13e0b98">
<input type="radio" value="radio_text_1" name="67ef5695" data-userkey="">

NOTE: These names will be the same between traveler instances unless the input was removed and re-created on the form and you have traveler instances of different versions.

Clone this wiki locally