In :# 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 :import fmrest fms = fmrest.Server('https://10.211.55.15', user='admin', password='admin', database='Contacts', layout='Demo', verify_ssl=False ) fms.login()
In :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 :name, type_, length, response = fms.fetch_file(record.portrait) name, type_, length
Out:('BF6B72F20D40DF69A18AD671A9FC978D0394145B6C20E7F54546DC213D8F0C97.png', 'image/png', '329139')
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
In :from IPython.display import Image Image(response.content)
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 :name, type_, length, response = fms.fetch_file(record.video, stream=True) name, type_, length
Out:('A744F78055D0E7AEA2DC873220AD8F54F6FC377CAC06D1E28C18BEB94196C594.mp4', 'video/mp4', '24642964')
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 :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