httpClient instance creation advice

I would like to get some input on a script library I'm writing to wrap ERP APIs and make them easy to use from Ignition. Right now, I have a class of type Transactor that will have methods within itself for specific requests. For instance, the stripped down version looks like this:

class Transactor():
    def __init__(self, transType):
        self.transType = transType
    
    def getWorkOrder(woNum):
        # make http call to endpoint to get work order

    def reportConsumption(item, quantity):
        # make http call to endpoint to post consumption

    ... etc ...

My question revolves around how often I should be creating an httpClient whenever I have this paradigm. I think I can probably create one httpClient in the constructor of the class and then use that instance for any methods. That way, on the consumer side, I can do something like

erpObj = shared.erp.Transactor('erpName')
erpObj.getWorkOrder('abc123')
...
erpObj.reportConsumption('a123312', 12.9)

The other way that I could do this (but seems like a bad idea) is to create a new httpClient within each method in my class. This seems unnecessary, but I don't fully understand the implications if I don't do it this way (this is the simpler way to do it in my mind, and how I built out a POC).

Any input / advice would be very helpful!

Instantiate it at the top level of the script library and use that one instance for all of your work.

1 Like

Dang... I know I read that somewhere but every search item I got for httpClient didn't bring up any relevant threads, at least from what I can see. I started down the road of using just one client and I think it'll be nice and clean. My setup is a little bit more complicated than this, since I have another class that inherits from Transactor and then a database configuration surface to manage the specific ERP transactors, but in essence, the problem definition is exactly what I stated.

Thank you for the quick link!