From 08da0f5665089de3b3efcd8be67ecd6911b2e34c Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sat, 19 Oct 2024 23:46:01 +0200 Subject: grocy update product --- elisp/grocy.el | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'elisp/grocy.el') diff --git a/elisp/grocy.el b/elisp/grocy.el index 90d96a7..6001a70 100644 --- a/elisp/grocy.el +++ b/elisp/grocy.el @@ -15,10 +15,13 @@ ;; ;;; Commentary: ;; -;; Grocy emacs frontend +;; Grocy Emacs frontend ;; ;;; Code: +(require 'url) +(require 'dash) + ;; Silence byte-compiler. (defvar url-http-end-of-headers) @@ -26,18 +29,18 @@ (defvar grocy-locations) (defcustom grocy-server "" - "Grocy main domain" + "Grocy main domain." :type 'string :group 'grocy) (defcustom grocy-api-key "" - "API key to authenticate to grocy server" + "API key to authenticate to grocy server." :type 'string :group 'grocy) (defun grocy--request (endpoint callback &optional method data) "Do a GET request to api ENDPOINT and call CALLBACK on the resulting buffer. -Set METHOD to other request types and include the alredy encoded DATA." +Set METHOD to other request types and include the already encoded DATA." (let ((url-request-method (or method "GET")) (url-request-extra-headers `(("GROCY-API-KEY" . ,grocy-api-key) @@ -49,6 +52,7 @@ Set METHOD to other request types and include the alredy encoded DATA." callback))) (defun grocy-update-units () + "Update localdb with units." (grocy--request "objects/quantity_units" (lambda (_status) @@ -64,6 +68,7 @@ Set METHOD to other request types and include the alredy encoded DATA." (setq grocy-units result))))) (defun grocy-update-locations () + "Update localdb with locations." (grocy--request "objects/locations" (lambda (_status) @@ -77,7 +82,11 @@ Set METHOD to other request types and include the alredy encoded DATA." (json-parse-buffer :object-type 'plist)) (setq grocy-locations result))))) +(grocy-update-units) +(grocy-update-locations) + (defun grocy-stock--row (row) + "Compose ROW into tabulated entry for stock view." (-let* (((&alist 'amount 'amount_opened 'product 'best_before_date) row) ((&alist 'name 'qu_id_stock) product) (amount @@ -116,6 +125,8 @@ Set METHOD to other request types and include the alredy encoded DATA." (display-buffer (current-buffer))))))) (defun grocy-products () + "Render tabulated list view of products." + (interactive) (grocy--request "objects/products" (lambda (_status) @@ -141,9 +152,49 @@ Set METHOD to other request types and include the alredy encoded DATA." (number-to-string min_stock_amount))))) result) (setq tabulated-list-entries)) + (local-set-key "e" #'grocy-update-product) (tabulated-list-init-header) (tabulated-list-print) (display-buffer (current-buffer))))))) +(defun grocy-update-product () + "Update form for currently selected product." + (interactive) + (let* ((product (tabulated-list-get-id)) + (id (gethash "id" product))) + (with-current-buffer (get-buffer-create "* Grocy update product *") + (erase-buffer) + (thread-first + product + (map-into 'alist) + (pp (current-buffer))) + (emacs-lisp-mode) + (keymap-local-set "C-c C-c" + (lambda () (interactive) + (grocy-update-object (format "products/%d" id)) + (kill-buffer))) + (switch-to-buffer (current-buffer))))) + +(defun grocy-buffer-obj->json (buffer) + "On the BUFFER read Lisp object and encode it into json." + (let ((json-null :null)) + (with-current-buffer buffer + (goto-char (point-min)) + (thread-first + (current-buffer) + (read) + (json-encode) + (encode-coding-string 'utf-8))))) + +(defun grocy-update-object (object-endpoint) + "Update on the OBJECT-ENDPOINT with the Lisp object on buffer." + (grocy--request + (format "objects/%s" object-endpoint) + (lambda (status) + (when status + (message "%S\n%S" status (buffer-string)))) + "PUT" + (grocy-buffer-obj->json (current-buffer)))) + (provide 'grocy) ;;; grocy.el ends here -- cgit v1.2.3