In [1]:
# hide ssl warnings for this example.
import requests
requests.packages.urllib3.disable_warnings()
This is a short example on how to fetch container data with python-fmrest.
In [2]:
import fmrest
fms = fmrest.Server('https://10.211.55.15',
user='admin',
password='admin',
database='Contacts',
layout='Demo',
verify_ssl=False
)
fms.login()
Out[2]:
In [3]:
record = fms.get_record(170)
# print locations of our two container fields
print(record.portrait)
print(record.video)
FileMaker Data API gives us a location to the asset in the container field. With the fetch_file() method, we can now access the data or get more informationen, such as size, about the file.
In [4]:
name, type_, length, response = fms.fetch_file(record.portrait)
name, type_, length
Out[4]:
We get back the filename (unfortunately, this is only the unique name the Data API exposes), and the content type and content length as delivered in the response header. The actual response of the request will be available in our response variable (find out more about requests response object: http://docs.python-requests.org/en/master/user/advanced/#request-and-response-objects)
So, we're looking at at 329kb jpeg file. We can access it with response.content
.
In [5]:
from IPython.display import Image
Image(response.content)
Out[5]:
When you have large container data you may not want to immediately load the file into memory, but first check the size or write it to disk in chunks. To do this, use the stream
argument, which will be passed on to the requests module.
In [6]:
name, type_, length, response = fms.fetch_file(record.video, stream=True)
name, type_, length
Out[6]:
Here we are looking at a mp4 video, which is 24 MB in size. We have access to type and length, but have not downloaded the content yet. (You have access to all response.headers
without loading the content.)
If you now decide to store that file, you can do it, for example, as follows:
In [7]:
with open('my_awesome_video.mp4', 'wb') as file_:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file_.write(chunk)
Note that if you only want to read partial data, you should eventually close the connection by response.close()
.