Loading a DataFrame from BOX API in python

As a temporary data source I want to use CSV files stored on box.com, and with this documentation it is super easy to do so!

I have made a class BOXFile.py:


from boxsdk import Client, OAuth2
from boxsdk.network.default_network import DefaultNetwork
from boxsdk.network.logging_network import LoggingNetwork
from boto.dynamodb.condition import NULL

class BOXFile:
 def __init__(self, CLIEND_ID, CLIENT_SECRET, ACCESS_TOKEN):
 self._oauth2 = OAuth2(CLIEND_ID, CLIENT_SECRET, access_token=ACCESS_TOKEN)
 self._client = Client(self._oauth2, LoggingNetwork())
 self._file = None

 def GetUserInfo(self):
 my = self._client.user(user_id='me').get()
 print my.name.encode("utf-8")
 print my.login.encode("utf-8")
 print my.avatar_url.encode("utf-8")
 def GetFile(self, fileID):
 self._file = self._client.file(fileID)
 return self._file.content()

On the object construction it logs requires login details into BOX (at this time to the DEV environment) and creates an _client property that we will be using in our further examples.

The Two methods: GetUserInfo and GetFile are first of many methods that I will code into the class as my needs grow.

So now lets get implementing first I am going to need some libraries:

from Model.BOX import BOXFile as bf
import pandas as pd
import io

Pandas I am going to use to create a DataFrame with the Data the io will convert the output from box into something pandas can read and of course the class needs to be implemented

boxfile = bf(CLIEND_ID='XXX', CLIENT_SECRET='XXXX', ACCESS_TOKEN='XXXXX')
csvFile = boxfile.GetFile('164280827785').decode("utf-8")
ioFile = io.StringIO(csvFile)

Like in the example I create an instance of the class loading OAuth2 and exposing the GetFile Method for myself, I get a CSV file and decode it, to turn it into an ioFile for Pandas, lastly:

df = pd.read_csv(filepath_or_buffer = ioFile, delimiter = ";")
print(df)

lastly we create our DataFrame using read_csv and print it!

Next we will encapsulate the whole process in two interacting classes.

Leave a comment