Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/basic/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def make_client():
except Exception as e:
print(e)

def make_client_https():
try:
# make Octopi connection via HTTPS with no verify certificate (i.e. selfsigned)
client = OctoRest(url="https://octopi.local", apikey="YouShallNotPass", verify=False)
return client
except Exception as e:
print(e)

def get_version():
client = make_client()
message = "You are using OctoPrint v" + client.version['server'] + "\n"
Expand Down
12 changes: 9 additions & 3 deletions octorest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional, Tuple
from enum import Enum
from time import sleep
import urllib3

import requests

Expand All @@ -23,12 +24,14 @@ class OctoRest:
Encapsulates communication with one OctoPrint instance
"""

def __init__(self, *, url=None, apikey=None, session=None):
def __init__(self, *, url=None, apikey=None, session=None, verify=True):
"""
Initialize the object with URL and API key

If a session is provided, it will be used (mostly for testing)
"""
self.verify = verify

if not url:
raise TypeError('Required argument \'url\' not found or empty')

Expand All @@ -45,6 +48,7 @@ def __init__(self, *, url=None, apikey=None, session=None):
if apikey:
self.load_api_key(apikey)


def load_api_key(self, apikey: str) -> None:
"""Use the given API key for all future communication with the OctoPrint server.

Expand Down Expand Up @@ -73,7 +77,8 @@ def _get(self, path, params=None):
Returns JSON decoded data
"""
url = urlparse.urljoin(self.url, path)
response = self.session.get(url, params=params)
if not self.verify : urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = self.session.get(url, params=params, verify=self.verify)
self._check_response(response)

return response.json()
Expand All @@ -90,7 +95,8 @@ def _post(self, path, data=None, files=None, json=None, ret=True):
Returns JSON decoded data
"""
url = urlparse.urljoin(self.url, path)
response = self.session.post(url, data=data, files=files, json=json)
if not self.verify : urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = self.session.post(url, data=data, files=files, json=json, verify=self.verify)
self._check_response(response)

if ret:
Expand Down