APIPythonOpen-Source Solutions

 View Only
  • 1.  Python create API session (newbie)

    Posted 06-10-2020 12:24:00 PM
    Hey, am trying to establish a python session to am stuck.

    import requests
    s = requests.Session()
    payload = {"type": "APISession", "version": {"type": "APIVersion", "major": 1, "minor": 10, "micro": 5}}
    url='http://host_name/resources/json/delphix/session'
    headers = {"content-type": "application/json"}
    
    d = s.post(url, data=payload, headers=headers)
    print(s.cookies)
    print(d.text)
    
    ----------------------------------------
    <RequestsCookieJar[<Cookie JSESSIONID=D2847C4763D84E880BA66DB427EB536F for host_name.local/resources>]>
    {"type":"ErrorResult","status":"ERROR","error":{"type":"APIError","details":"Syntax error in JSON input: Unrecognized token 'version': was expecting ('true', 'false' or 'null')\n at [Source: (BufferedInputStream); line: 1, column: 9]","action":"Correct the error and try again.","id":"exception.webservices.api.input.invalid.json","commandOutput":null,"diagnoses":[]}}


    Am not sure what is that am missing here.

    ------------------------------
    Srini
    ------------------------------


  • 2.  RE: Python create API session (newbie)
    Best Answer

    Posted 06-10-2020 12:55:00 PM
    Hello Srini,

    You may want to take a look at our Python SDK available on Pipy (pip install delphixpy). It has all of the APIs in Python so you don't have to format JSON requests. We have a GitHub repo with Python3 (https://github.com/delphix/delphixpy-examples/blob/python3-api-1-10-2/lib/get_session.py) and Python2 (https://github.com/delphix/delphixpy-examples).

    Here's an example of getting a session using delphixpy:
    https://github.com/delphix/delphixpy-examples/blob/python3-api-1-10-2/lib/get_session.py


    Otherwise, you will need to POST username, password, namespace in the payload.

    ------------------------------
    Corey Brune
    Technical Manager
    Delphix
    ------------------------------



  • 3.  RE: Python create API session (newbie)

    Posted 06-11-2020 01:59:00 PM
    The payload variable just needs to be single quoted.

    $ python --version
    Python 2.7.10

    $ more test.py
    import requests
    s = requests.Session()
    payload = '{"type": "APISession", "version": {"type": "APIVersion", "major": 1, "minor": 10, "micro": 5}}'
    url='http://172.16.160.195/resources/json/delphix/session'
    headers = {"content-type": "application/json"}
    d = s.post(url, data=payload, headers=headers)
    print(s.cookies)
    print(d.text)

    $ python test.py
    <RequestsCookieJar[<Cookie JSESSIONID=FD93A1B19AE03029735756FC968A4E52 for 172.16.160.195/resources>]>
    {"type":"OKResult","status":"OK","result":{"type":"APISession","version":{"type":"APIVersion","major":1,"minor":10,"micro":5},"locale":null,"client":null},"job":null,"action":null}
    $

    ------------------------------
    Alan Bitterman
    Principal Solutions Engineer
    Delphix
    ------------------------------



  • 4.  RE: Python create API session (newbie)

    Posted 06-12-2020 02:10:00 PM
    That worked Alan and now am stuck at next step.

    My ID has DE Admin privileged and I tried with no suffix, 'DOMAIN' and 'SYSTEM' and still getting same error. 

    payload = '{"type": "LoginRequest", "username": "user_name@DOMAIN", "password": "password"}'
    url='http://host_name/resources/json/delphix/login'
    l=s.post(url, data=payload, headers=headers)
    print(l.text)
    
    {"type":"ErrorResult","status":"ERROR","error":{"type":"APIError","details":"Invalid username or password.","action":"Try with a different set of credentials.","id":"exception.webservices.login.failed","commandOutput":null,"diagnoses":[]}}


    ------------------------------
    Srini
    ------------------------------



  • 5.  RE: Python create API session (newbie)

    Posted 06-12-2020 02:28:00 PM
    Update - previously I tried my ldap ID and had issue.

    Now I created a local user (DE Admin user) and it worked - what should we do to get the ldap ID working?


    ------------------------------
    Srini
    ------------------------------



  • 6.  RE: Python create API session (newbie)

    Posted 06-12-2020 10:27:00 PM

    I had to add suffix in separate key/value pair as below and it worked

    l_payload = '{"type": "LoginRequest", "username": "redacted ", "password": "redacted", "target": "DOMAIN"}'



    ------------------------------
    Srini
    ------------------------------