Skip to content

Instantly share code, notes, and snippets.

@shacker
Last active July 17, 2024 14:55
Show Gist options
  • Save shacker/87908e13c9ee6655ce90 to your computer and use it in GitHub Desktop.
Save shacker/87908e13c9ee6655ce90 to your computer and use it in GitHub Desktop.

Revisions

  1. shacker revised this gist Jun 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,8 @@
    anyone finds it helpful.
    The most common SOAP lib for Python is suds, but suds has fallen out of maintenance as
    SOAP has fallen out of favor the years. suds is officially replaced by suds-jurko, which is
    now part of Fedora:
    SOAP has fallen out of favor over the past 5-10 years.
    suds is officially replaced by suds-jurko, which is now part of Fedora:
    https://bitbucket.org/jurko/suds
    https://fedorahosted.org/suds/wiki/Documentation
  2. shacker revised this gist Jun 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@
    username = 'username@yourtenant'
    password = 'xxxxxxxxxxxxxxxxxx'

    wsdl_url = 'https://wd5-impl-services1.workday.com/ccx/service/cca/Human_Resources/v24.1?wsdl'
    wsdl_url = 'https://wd5-impl-services1.workday.com/ccx/service/[yourtenant]/Human_Resources/v24.1?wsdl'
    Employee_ID = '123456' # Replace with a known user ID in your tenant
    client = client.Client(wsdl_url)

  3. shacker revised this gist Jun 1, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,8 @@
    https://bitbucket.org/jurko/suds
    https://fedorahosted.org/suds/wiki/Documentation
    pip install suds-jurko
    '''

    # Uncomment for full debug output:
  4. shacker created this gist Jun 1, 2015.
    95 changes: 95 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    import sys

    from suds import client
    from suds.wsse import Security, UsernameToken
    from suds.sax.text import Raw
    from suds.sudsobject import asdict
    from suds import WebFault

    '''
    Given a Workday Employee_ID, returns the last name of that employee.
    I had trouble finding working examples online of interacting with the Workday SOAP API
    via Python - both the authentication piece and data retrieval. It turns out to be very simple,
    but it took a while to come up with due to scant documentation, so posting here in case
    anyone finds it helpful.
    The most common SOAP lib for Python is suds, but suds has fallen out of maintenance as
    SOAP has fallen out of favor the years. suds is officially replaced by suds-jurko, which is
    now part of Fedora:
    https://bitbucket.org/jurko/suds
    https://fedorahosted.org/suds/wiki/Documentation
    '''

    # Uncomment for full debug output:
    # import logging
    # logging.basicConfig(level=logging.INFO)
    # logging.getLogger('suds.client').setLevel(logging.DEBUG)
    # logging.getLogger('suds.transport').setLevel(logging.DEBUG)
    # logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
    # logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)

    # Fully credentialed service user with access to the Human Resources API
    username = 'username@yourtenant'
    password = 'xxxxxxxxxxxxxxxxxx'

    wsdl_url = 'https://wd5-impl-services1.workday.com/ccx/service/cca/Human_Resources/v24.1?wsdl'
    Employee_ID = '123456' # Replace with a known user ID in your tenant
    client = client.Client(wsdl_url)

    # Wrapping our client call in Security() like this results in submitting
    # the auth request with PasswordType in headers in the format WD expects.
    security = Security()
    token = UsernameToken(username, password)
    security.tokens.append(token)
    client.set_options(wsse=security)

    # The workflow is, generate an XML element containing the employee ID, then post
    # that element to the Get_Workers() method in the WSDL as an argument.
    # We could do this with two suds calls, having it generate the XML from the schema,
    # but here I'm creating the POST XML manually and submitting it via suds's `Raw()` function.
    xmlstring = '''
    <ns0:Worker_Reference>
    <ns0:ID ns0:type="Employee_ID">{id}</ns0:ID>
    </ns0:Worker_Reference>
    '''.format(id=Employee_ID)

    xml = Raw(xmlstring)

    try:
    result = client.service.Get_Workers(xml)
    except WebFault as e:
    # Employee ID probably doesn't exist.
    print(e)
    sys.exit()

    # ===================
    # That's essentially all you need. Everything below is just response parsing.
    # ===================


    # Converts the unusually formatted response object to standard Python dictionary.
    # You'll probably want to move this into a utils.py and import it.
    def recursive_asdict(d):
    """Convert Suds object into serializable format."""
    out = {}
    for k, v in asdict(d).iteritems():
    if hasattr(v, '__keylist__'):
    out[k] = recursive_asdict(v)
    elif isinstance(v, list):
    out[k] = []
    for item in v:
    if hasattr(item, '__keylist__'):
    out[k].append(recursive_asdict(item))
    else:
    out[k].append(item)
    else:
    out[k] = v
    return out

    worker_dict = recursive_asdict(result)
    worker = worker_dict['Response_Data']['Worker'][0]['Worker_Data']
    lname = worker['Personal_Data']['Name_Data']['Legal_Name_Data']['Name_Detail_Data']['Last_Name']

    print(lname)