The ability to both read and write Attachments to Devices and Wells and Shape Files for Map View with the eLynx API is available. Unfortunately the automated process that builds the documentation in the API portal does not do a very good job of documenting the usage of the specific API's, or of generating example code for those API's so this document will walk through the process of exercising all four of these functions with working code in Python.
Attachments
Get Attachments
Get Attachments returns a JSON array of Attached documents. Each item in the array contains a File Name, a File Description, and an URL that can be used to download the actual file. Here is the structure of the JSON Array:
[{ "fileName": "string", "description": "string", "attachmentUrl": "string" }]
Here is an example program in Python to download the files:
########### Python 3.2 #############
import urllib.request, json
try:
url = "https://api-dev.elynxtech.com/v1/attachments?objectId={objectId}" # Add ObjectID from GetDevices or GetWells here
hdr ={
# Request headers
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '{SubscriptionKey}', #Add your subscription key here.
}
req = urllib.request.Request(url, headers=hdr)
req.get_method = lambda: 'GET'
response = urllib.request.urlopen(req)
body = response.read()
jsonList = json.loads(body) #Convert response to dictionary
#print(jsonList)
for fileItem in jsonList: #For every item in the dictionary extract it and download the file.
print('Downloading', fileItem.get('fileName'))
req = urllib.request.Request(fileItem.get('attachmentUrl'))
req.get_method = lambda: 'GET'
fileResponse = urllib.request.urlopen(req) #Request is built, download file.
# Ensure the request was successful
if fileResponse.getcode() == 200:
# Access the binary content
binary_data = fileResponse.read()
# Save the binary data to a local file (optional)
with open(fileItem.get('fileName'), "wb") as file: #Example code - saves to current working directory.
file.write(binary_data)
except Exception as e:
print(e)
####################################
Note: This code sample gets the list of files, iterates over the list, and downloads each file to the current working directory.
Add Attachments
Attachments may be added to any Device or Well for which you have the appropriate permissions.
Here is an example Python program that uploads an attachment.
########### Python 3.2 #############
import requests, json
from requests_toolbelt.multipart.encoder import MultipartEncoder
url = "https://api-dev.elynxtech.com/v1/attachments"
# Open the file in binary mode
file_path1 = r'{FileToAttach}' #Path and name of file to attach
with open(file_path1, "rb") as file1:
# Prepare the files for upload
files = {"file": file1}
# Set Object ID to the GUID for the object you wish to attach a file to.
# Set ObjectType to 'Device' or 'Well' as necessary
# Set the duplicationAction to one of: 'Overwrite', 'ThrowException', or 'AddSuffix'
# Set the description to any informative screen
# DO NOT CHANGE attachmentType
data = {'objectId': '{objectId}', 'objectType': '{Type}', 'duplicationAction': '{Action}', 'description' : '{description}', 'attachmentType': 'Attachments'}
hdr = {
# Request headers
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '{SubscriptionKey}'
}
# Send the POST request with the file
response = requests.post(url, data=data, files=files, headers=hdr)
# Check the response
if response.status_code == 200:
print("File uploaded successfully!")
else:
print(f"Failed to upload file. Status code: {response.status_code}")
Note that the file attributes are built into a data block and the file itself is added to a file block. This code uploads and attaches a single file to an object in the eLynx Application.
Get Shape Files
Shape files (KML, KMZ or SHP/PRJ pairs) are read using the Get Shape Files API call. This call returns a response with the same properties as the Get Attachments call, therefore we can read the file list and download with a similar Python program:
########### Python 3.2 #############
import urllib.request, json
try:
# Set "IncludeKmzAndKml" and "IncludeShpAndPrj" to true or false to download only those types, as shown below ALL shape files will download.
# Add the appropriate {organizationId} from GetOrangizations
url = "https://api-dev.elynxtech.com/v1/organizations/{organizationId}/gisfiles?IncludeKmzAndKml=true&IncludeShpAndPrj=true"
hdr ={
# Request headers
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '{subscriptionKey}', #Add your Subscription-Key here
}
req = urllib.request.Request(url, headers=hdr)
req.get_method = lambda: 'GET'
response = urllib.request.urlopen(req)
body = response.read()
jsonList = json.loads(body) #Convert response to dictionary
print(jsonList)
for fileItem in jsonList: #For every item in the dictionary extract it and download the file.
print('Downloading', fileItem.get('fileName'))
req = urllib.request.Request(fileItem.get('attachmentUrl'))
req.get_method = lambda: 'GET'
fileResponse = urllib.request.urlopen(req) #Request is built, download file.
# Ensure the request was successful
if fileResponse.getcode() == 200:
# Access the binary content
binary_data = fileResponse.read()
# Save the binary data to a local file (optional)
with open(fileItem.get('fileName'), "wb") as file: #Example code - saves to current working directory.
file.write(binary_data)
except Exception as e:
print(e)
####################################
This sample python code reads all shape files for and organization and saves them to the current working directory.
Upload Shape Files
Shape files may be uploaded to the eLynx Application via the API. The shape files are placed into a dictionary object and that object is uploaded through the requests.post command.
########### Python 3.2 #############
import requests, json
from requests_toolbelt.multipart.encoder import MultipartEncoder
url = "https://api-dev.elynxtech.com/v1/organizations/{organizationId}/gisfiles"
# Open the file in binary mode
file_path1 = r'{ShapeFileToUpload}' #Shape File
file_path2 = r'{ProjectionFileToUpload}' #Projection File
with open(file_path1, "rb") as file1, open(file_path2, "rb") as file2:
# Prepare the files for upload
files = {"file1": file1, "file2": file2}
hdr = {
# Request headers
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '{SubscriptionKey}'
}
# Send the POST request with the file
response = requests.post(url, files=files, headers=hdr)
# Check the response
if response.status_code == 200:
print("File uploaded successfully!")
else:
print(f"Failed to upload file. Status code: {response.status_code}")
This python example takes 1 .SHP file and 1 matching .PRJ file and uploads them to the specified organization.
Comments
0 comments
Please sign in to leave a comment.