ONTAP Rest API Discussions

how do i create a session object with Python ontap rest api

kbhonagiri
1,628 Views

Hi Everyone,

 

How do i create an ONTAP session object with username/password and use it for other rest API calls? Can anyone provide an example with connection object

 

def session(self) -> requests.Session:
"""A `requests.Session` object which is used for all API calls.

This session is reused for each API call made with this connection. Multiple
requests may therefore be sent through the same TCP connection assuming
the host supports keep-alive.

Returns:
A `requests.Session` object which is used for all API calls.
"""

 

1 ACCEPTED SOLUTION

ddegraaf
1,586 Views

Hi,

Instead of using the specific resource object to make REST API calls, you can use the ONTAP session object.

Here is an example of using the session object with a username and password to make a simple get call:

 

from netapp_ontap import HostConnection

from netapp_ontap.resources import Cluster



connection = HostConnection(host, username="username", password="password", verify=False)

response = connection.session.get(f'https://{host}/api/cluster/')

print(f'Status Code: {response.status_code}, Content: {response.json()}')

View solution in original post

4 REPLIES 4

ddegraaf
1,587 Views

Hi,

Instead of using the specific resource object to make REST API calls, you can use the ONTAP session object.

Here is an example of using the session object with a username and password to make a simple get call:

 

from netapp_ontap import HostConnection

from netapp_ontap.resources import Cluster



connection = HostConnection(host, username="username", password="password", verify=False)

response = connection.session.get(f'https://{host}/api/cluster/')

print(f'Status Code: {response.status_code}, Content: {response.json()}')

kbhonagiri
1,570 Views

Thank you. How can i use python client library using session object instead of making direct rest API call ?

ddegraaf
1,568 Views

I might be misunderstanding your question, but another way to write my previous code above using the specific Cluster object would be:

from netapp_ontap import HostConnection

from netapp_ontap.resources import Cluster

logging.basicConfig(level=logging.DEBUG)

utils.DEBUG=1


with HostConnection(host, username='username', password='password', verify=False):

cluster = Cluster()

cluster.get()

print(cluster)

 

You can find more examples for all the netapp_ontap PCL resources here: https://library.netapp.com/ecmdocs/ECMLP2884819/html/resources/index.html

 

Does that answer your question?

 

kbhonagiri
1,506 Views

Thanks, this helped

response = connection.session.get(f'https://{host}/api/cluster/')

 

Public