Step-by-step guide to authenticate with the Enterprise File Fabric API using Python

Over the past week I’ve been busy creating Python scripts to automate certain processes here in the SME DevOps team.

One of my main objectives has been to create a script that interacts with the SME API to check the health of specific functions, for this task I decided to opt for the language of ‘DevOps’ Python. Whilst I do not profess to be a Python expert in any way I felt that this is a language is one that I have become accustomed too and am able to ‘get by’ with. The nice thing with Python are the extensive libraries that are readily available, and the fact that they are fairly easy to use (of course, only if you have some background experience with Python).

In this short ‘POST’, I’m going to get you authenticated with the SME API and running some very basic ‘requests’, apologies for the puns.

Firstly, I am using Python 2.7 and have installed the requests library. More on requests can be found here http://docs.python-requests.org/en/master/, to install requests you will need to use pip.

Let’s make an initial connection to the API and get ourselves authenticated. Using your preferred text editor create a new python file and import the following libraries. (I use Github’s atom https://atom.io), create a new python file and import the following libraries.


import requests
import base64, re

You can see from above we first import the requests library and then import the base64 library. It is necessary to import the base64 library as the SME API encodes authentication data into base64 format. Below I have provided an example of basic base64 encoding. One last library is the re library for regular expressions, we will only use this for it’s search method, which is a bit like ‘grep’.


user = base64.b64encode(‘UserName’)
password = base64.b64encode(‘Password’)
payload = user+’,’+password
url = ‘https://storagemadeeasy.com/api/’

Above you’ll see the user/password encoding and two final pieces, one, that’s the payload, which is how we will deliver the credentials, and two a new variable called url, which is excatly as it suggests and contains the location of the SME API.
Now we’ll need to connect to the SME API and create a new session token. Below is an extract from some code I have been working on.


go = requests.get(url+'*/gettoken/'+payload).text
grab = go.strip().split()
for line in grab:
    if re.search(‘’, line):
        token = line[7:-8]
        return token

Let’s break that down a little. In the first instance we use the get method from the requests library to authenticate to SME. You can see we are combining the url variable with the SME API function */gettoken/ then finally joining the payload we created earlier to deliver the credentials. The final part of this line is calling on the .text method from the requests library to output XML content from SME.

Next up we create a new variable from the initial response called grab, this uses the strip method an split method from the standard Python libraries. If you are aware of strip and split you will know that strip called without any characters removes whitespaces and split, splits on all whitespace if left unspecified.

We should now iterate over the results until we find the word , which we can now stop on and finally return the result minus the first 7 and last 8 characters which would be the words and . We now have our generated session token of which we can use to upload/download, create/delete edit/move files and folders.
Below is a finished example of how to authenticate with SME API.


import request
import base64, re

user = base64.b64encode(‘UserName’)
password = base64.b64encode(‘Password’)
payload = user+’,’+password
url = ‘https://storagemadeeasy.com/api/’

def authApi():
    go = requests.get(url+'*/gettoken/'+payload).text
    grab = go.strip().split()
    for line in grab:
        if re.search('', line):
            token = line[7:-8]
            return token 

authApi()

By all means, if you have any better modifications or better suggestions on how to use the API with python, please try them.

This blog was intended to serve as a starting point for those who need a little guidance on how to start using SME via it’s API and should not be taken as ‘the only way’ of authenticating. For more information on the functions SME use within it’s API, please take a look at the following API guide https://storagemadeeasy.com/for_developers/.

 

Facebooktwitterredditpinterestlinkedinmailby feather
The following two tabs change content below.
The Leading Enterprise File Fabric