Help with Web API Calls

Right, so the content-type out of Ignition is going to be application/json unless you jump through some hoops (for now) - see this post for some useful details:

Here's the thing. Notice the example success response to the token request has an 'expires_in' field:
image
Assuming that's seconds (undocumented) and assuming it's 3600 seconds in a real response (again, undocumented :slight_smile:) your token will last exactly an hour. The proper way to utilize an OAuth API is to authorize once, then continuously refresh your token indicating you're continuing to use it. What will happen if you just re-authenticate "from scratch" each time you use the API is up to the server, really.

If this is a one-off (there's only one place you will ever be retrieving this data, and you don't expect it to apply to more sites) then there's not a huge advantage to the class based syntax. For clarity, I was picturing something like this:

class EasyRxClient(object):
	def __init__(self, clientId, clientSecret, baseUrl = "https://staging.easyrxortho.com/"):
		self.baseUrl = baseUrl
		self.clientId = clientId
		self.clientSecret = clientSecret
		self.client = system.net.httpClient()
		self.__refreshToken = None
		self.__accessToken = None

	def getOrUpdateAccessToken(self):
		# have some conditional logic, e.g. based on token expiry, to request a new access token/refresh the existing token here
		return __accessToken;

	def getOpenCases(self):
		token = self.getOrUpdateAccessToken()
		return self.client.get(self.baseUrl + "/webservice/get_open_cases")

Which has the advantage of being much cleaner at use site - wherever you care about this data, it's trivial to retrieve:

easyRx = EasyRxClient(clientId, clientSecret)
openCases = easyRx.getOpenCases()

But I don't know your environment, your business needs, how comfortable you & your organization are with Python, how much this might need to change over time, etc. That's ultimately a judgement call for you to make, I just mentioned it as a syntactic possibility.

2 Likes