Http bindings aren’t going to work well for this purpose - they’re not very good at authentication right now.
I’d recommend system.net.httpClient()
. You could wrap things in a class to make accessing the API a little friendlier:
class PortainerClient(object):
def __init__(self, baseUrl, username, password, **kwargs):
self.baseUrl = baseUrl
self.client = system.net.httpClient(**kwargs)
response = self.client.post(baseUrl + "auth", data={"Username": username, "Password": password})
self.token = response.json["jwt"]
def call(self, endpoint, method="GET", **kwargs):
headers = kwargs.get("headers", {})
headers["Authorization"] = "Bearer {}".format(self.token)
response = self.client.request(self.baseUrl + endpoint, method, headers=headers, **kwargs)
return response.json
client = PortainerClient("https://docker.ia.local/api/", "admin", "password", bypass_cert_validation=True)
print client.call("endpoints/1/docker/containers/json")