#24 API Token Support

This commit is contained in:
David Zingg 2020-07-01 21:19:53 +02:00
parent be81f64ec1
commit b3e53821bb
2 changed files with 27 additions and 15 deletions

View File

@ -4,7 +4,6 @@
<list default="true" id="7e2e0eec-b22e-4d48-8f24-196d1ed9b51a" name="Default Changelist" comment=""> <list default="true" id="7e2e0eec-b22e-4d48-8f24-196d1ed9b51a" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/octoprint_mystromswitch/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/octoprint_mystromswitch/__init__.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/octoprint_mystromswitch/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/octoprint_mystromswitch/__init__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2" beforeDir="false" afterPath="$PROJECT_DIR$/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -49,13 +48,6 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1574193087583</updated> <updated>1574193087583</updated>
</task> </task>
<task id="LOCAL-00049" summary="#14 Einstellungen reorganisieren&#10;&#10;- Beschreibungen angepasst">
<created>1574874286229</created>
<option name="number" value="00049" />
<option name="presentableId" value="LOCAL-00049" />
<option name="project" value="LOCAL" />
<updated>1574874286229</updated>
</task>
<task id="LOCAL-00050" summary="#15 Beschreibung erstellen"> <task id="LOCAL-00050" summary="#15 Beschreibung erstellen">
<created>1574876224274</created> <created>1574876224274</created>
<option name="number" value="00050" /> <option name="number" value="00050" />
@ -392,7 +384,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1593630227011</updated> <updated>1593630227011</updated>
</task> </task>
<option name="localTasksCounter" value="98" /> <task id="LOCAL-00098" summary="#24 API Token Support">
<created>1593630650365</created>
<option name="number" value="00098" />
<option name="presentableId" value="LOCAL-00098" />
<option name="project" value="LOCAL" />
<updated>1593630650365</updated>
</task>
<option name="localTasksCounter" value="99" />
<servers /> <servers />
</component> </component>
<component name="UnknownFeatures"> <component name="UnknownFeatures">
@ -422,7 +421,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="#5 Rest Api integration&#10;&#10;- Bugfix plugin_requires" />
<MESSAGE value="#5 Rest Api integration&#10;&#10;- Nullpointer fix" /> <MESSAGE value="#5 Rest Api integration&#10;&#10;- Nullpointer fix" />
<MESSAGE value="#6 UI Updates senden und anzeigen&#10;&#10;- unnötigen Code entfernen, erster Versuch" /> <MESSAGE value="#6 UI Updates senden und anzeigen&#10;&#10;- unnötigen Code entfernen, erster Versuch" />
<MESSAGE value="Revert &quot;#6 UI Updates senden und anzeigen&quot;&#10;&#10;This reverts commit f83d4259" /> <MESSAGE value="Revert &quot;#6 UI Updates senden und anzeigen&quot;&#10;&#10;This reverts commit f83d4259" />
@ -447,6 +445,7 @@
<MESSAGE value="#21 Add PowerOFF when print is finished&#10;&#10;neue Einstellmöglichkeiten anzeigen" /> <MESSAGE value="#21 Add PowerOFF when print is finished&#10;&#10;neue Einstellmöglichkeiten anzeigen" />
<MESSAGE value="#22 Button taucht nicht auf&#10;&#10;Fehler anzeigen wenn Verbindung zu switch nicht klappt" /> <MESSAGE value="#22 Button taucht nicht auf&#10;&#10;Fehler anzeigen wenn Verbindung zu switch nicht klappt" />
<MESSAGE value="#21 Add PowerOFF when print is finished" /> <MESSAGE value="#21 Add PowerOFF when print is finished" />
<option name="LAST_COMMIT_MESSAGE" value="#21 Add PowerOFF when print is finished" /> <MESSAGE value="#24 API Token Support" />
<option name="LAST_COMMIT_MESSAGE" value="#24 API Token Support" />
</component> </component>
</project> </project>

View File

@ -177,8 +177,11 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
if self.ip is not None: if self.ip is not None:
try: try:
try: try:
headers = {}
if self.token is not None and self.token != "":
headers = {"Token": self.token}
request = requests.get( request = requests.get(
'http://{}/report'.format(self.ip), timeout=1) 'http://{}/report'.format(self.ip), headers=headers, timeout=1)
if request.status_code == 200: if request.status_code == 200:
timestamp = time.time() timestamp = time.time()
data = request.json() data = request.json()
@ -216,8 +219,11 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
value = '1' value = '1'
while nbRetry < 3: while nbRetry < 3:
try: try:
headers = {}
if self.token is not None and self.token != "":
headers = {"Token": self.token}
request = requests.get( request = requests.get(
'http://{}/relay'.format(self.ip), params={'state': value}, timeout=1) 'http://{}/relay'.format(self.ip), params={'state': value}, headers=headers, timeout=1)
if request.status_code == 200: if request.status_code == 200:
return return
else: else:
@ -240,8 +246,12 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
try: try:
self._logger.info("try to send Powercycle Request") self._logger.info("try to send Powercycle Request")
self._logger.info('http://{}/timer'.format(self.ip)) self._logger.info('http://{}/timer'.format(self.ip))
headers = {}
if self.token is not None and self.token != "":
headers = {"Token": self.token}
request = requests.post( request = requests.post(
'http://{}/timer'.format(self.ip), params={'mode': value, 'time': time}, timeout=1) 'http://{}/timer'.format(self.ip), params={'mode': value, 'time': time}, headers=headers,
timeout=1)
if request.status_code == 200: if request.status_code == 200:
return return
else: else:
@ -258,8 +268,11 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
nbRetry = 0 nbRetry = 0
while nbRetry < 3: while nbRetry < 3:
try: try:
headers = {}
if self.token is not None and self.token != "":
headers = {"Token": self.token}
request = requests.get( request = requests.get(
'http://{}/toggle'.format(self.ip), timeout=1) 'http://{}/toggle'.format(self.ip), headers=headers, timeout=1)
if request.status_code == 200: if request.status_code == 200:
return return
else: else: