feat(label): Switch to POST request

This commit is contained in:
Andreas Mieke 2024-04-21 21:55:38 -04:00
parent 1cf5a58807
commit 85981d81b5
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8

View file

@ -5,8 +5,7 @@ from django.core.validators import MaxValueValidator
from django.http import JsonResponse
# printer support
import socket
import json
import requests
# barcode helpers
from InvenTree.helpers_model import getModelsWithMixin
@ -28,15 +27,10 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
TITLE = "Phomemo Label Printer"
SETTINGS = {
'IP_ADDRESS': {
'name': _('IP Address'),
'description': _('IP address of phomemo print server'),
'default': '',
},
'PORT': {
'name': _('Port'),
'description': _('Port of phomemo print server'),
'default': '9100',
'URL': {
'name': _('URL'),
'description': _('URL of the print endpoint'),
'default': 'http://localhost:8080/print',
},
}
@ -85,21 +79,11 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
outputs.append(self.get_fields(label))
# Read settings
ip_address = self.get_setting('IP_ADDRESS')
port = int(self.get_setting('PORT'))
# Dump to json
data = json.dumps(outputs)
url = self.get_setting('URL')
# Send the label to the printer
try:
print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print_socket.settimeout(5)
print_socket.connect((ip_address, port))
print_socket.sendall(bytes(data,encoding="utf-8"))
print_socket.close()
except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error))
r = requests.post(url, json=outputs)
r.raise_for_status()
return JsonResponse({
'success': True,