This file contains methods for getting an OAuth Token, as well as for interacting with all of the MarketSuite APIs. Methods that return an xml or csv payload will save that payload as a file (file paths are variable in info.py).
import json
import http.client
import base64
import urllib.parse
import mimetypes
from codecs import encode
def get_token(url, username, password, clientId, secret):
conn = http.client.HTTPSConnection(url)
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=grant_type;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode("password"))
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=username;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode(username))
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=password;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode(password))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
clientIdAndSecret = clientId + ':' + secret
encodedClientIdAndSecret = base64.b64encode(clientIdAndSecret.encode('ascii'))
headers = {
'Authorization': 'Basic ' + encodedClientIdAndSecret.decode('ascii'),
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/oauth/token", payload, headers)
res = conn.getresponse()
data = res.read().decode("utf-8")
tokenInfo = json.loads(data)
return tokenInfo["access_token"]
def get_status(url, token, fileIdentifier):
conn = http.client.HTTPSConnection(url)
payload = ''
headers = {
'Authorization': 'Bearer ' + token
}
conn.request("GET", "/fileRegistry/file/" + fileIdentifier + "/status", payload, headers)
res = conn.getresponse()
data = res.read()
return data
def list_files(url, token, region, participant, fileType, source, rendition, lastestVersionOnly, intervalBegin, intervalEnd):
conn = http.client.HTTPSConnection(url)
payload = json.dumps({
"region": region,
"participant": participant,
"fileType": fileType,
"source": source,
"rendition": rendition,
"latestVersionOnly": lastestVersionOnly
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
conn.request("PUT", "/fileRegistry/listFiles?intervalBegin=" + intervalBegin + "&intervalEnd=" + intervalEnd, payload, headers)
res = conn.getresponse()
response = res.read()
return response.decode("utf-8")
def list_files_since(url, token, region, participantShortName, fileType, source, rendition, lastestVersionOnly, watermarkDate):
conn = http.client.HTTPSConnection(url)
payload = json.dumps({
"region": "" + region + "",
"participant": "" + participantShortName + "",
"fileType": fileType,
"source": source,
"rendition": rendition,
"latestVersionOnly": lastestVersionOnly
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
conn.request("PUT", "/fileRegistry/listFilesSince?watermarkDate=" + watermarkDate, payload, headers)
res = conn.getresponse()
response = res.read()
return response.decode("utf-8")
def get_report(url, token, payload, reportPath, reportFormat):
conn = http.client.HTTPSConnection(url)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
reportPathEncoded = urllib.parse.quote(reportPath)
conn.request("PUT", "/reporting/getReport?reportPath=" + reportPathEncoded + "&reportFormat=" + reportFormat, payload, headers)
res = conn.getresponse()
return res.read()
def get_file(url, token, fileIdentifier):
conn = http.client.HTTPSConnection(url)
payload = ''
headers = {
'Authorization': 'Bearer ' + token
}
conn.request("GET", "/fileRegistry/file/" + fileIdentifier, payload, headers)
res = conn.getresponse()
data = res.read()
return data
def get_schedule_data(url, token, payload, fileLocation, fileName):
conn = http.client.HTTPSConnection(url)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
}
fileNameEncoded = urllib.parse.quote(fileName)
conn.request("PUT", "/fileRegistry" + fileLocation + "?fileName=" + fileNameEncoded, payload, headers)
res = conn.getresponse()
data = res.read()
return data
def upload_file(url, token, fileName, fileAttributes):
conn = http.client.HTTPSConnection(url)
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=file; filename={0}'.format(fileName)))
fileType = mimetypes.guess_type(fileName)[0] or 'application/octet-stream'
dataList.append(encode('Content-Type: {}'.format(fileType)))
dataList.append(encode(''))
with open(fileName, 'rb') as f:
dataList.append(f.read())
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=description;'))
#dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode(fileAttributes))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
# print(body)
payload = body
headers = {
'Authorization': 'Bearer ' + token,
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/fileRegistry/file", payload, headers)
res = conn.getresponse()
data = res.read()
return data