From 4fd1372b425500f1afe11301c2468f2099bacfde Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Thu, 11 Jan 2024 21:41:38 +0100 Subject: [PATCH] Add ability to return stubbed responses When developing / debugging, it's a waste of API credit to make actual calls. Flip the kagi-stubbed-responses flag to t to enable dummy responses. --- README.org | 1 + kagi.el | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 847490a..443432e 100644 --- a/README.org +++ b/README.org @@ -102,6 +102,7 @@ The token can be supplied directly as a string, but you could write a lambda to |----------------------------------+---------------------------------------------------------| | kagi-api-token | The Kagi API token. | | kagi-fastgpt-api-url | The Kagi FastGPT API entry point. | +| kagi-stubbed-responses | Whether the package should return a stubbed response. | | kagi-summarizer-api-url | The Kagi Summarizer API entry point. | | kagi-summarizer-cache | Determines whether the Summarizer should cache results. | | kagi-summarizer-default-language | Default target language of the summary. | diff --git a/kagi.el b/kagi.el index acff43c..3e42f45 100644 --- a/kagi.el +++ b/kagi.el @@ -63,6 +63,14 @@ https://kagi.com/settings?p=api" :type '(choice string function) :group 'kagi) +(defcustom kagi-stubbed-responses nil + "Whether the package should return a stubbed response. + +To be used for testing purposes, such that no credits are spent +on dummy data." + :type 'boolean + :group 'kagi) + (defcustom kagi-fastgpt-api-url "https://kagi.com/api/v0/fastgpt" "The Kagi FastGPT API entry point." :type '(choice string function) @@ -206,6 +214,35 @@ https://help.kagi.com/kagi/api/fastgpt.html for more information." "--header" "Content-Type: application/json" "--data" "@-"))) +(defvar kagi--fastgpt-stubbed-response + "{\"data\":{\"output\":\"abc\"}}" + "Stubbed response for the Kagi FastGPT endpoint.") + +(defvar kagi--summarizer-stubbed-response + "{\"data\":{\"output\":\"a```b```c\"}}" + "Stubbed response for the Kagi Summarizer endpoint.") + +(defun kagi--call-process-region (&rest args) + "`call-process-region' wrapper. + +Calls `call-process-region' with ARGS, unless +`kagi-stubbed-responses' is non-nil. + +In that case, this function will not do an actual API call but +return some dummy data." + (if (not kagi-stubbed-responses) + (apply #'call-process-region args) + (let* ((url (car (last args))) + (response (cond + ((string= url kagi-fastgpt-api-url) + kagi--fastgpt-stubbed-response) + ((string= url kagi-summarizer-api-url) + kagi--summarizer-stubbed-response) + (t "")))) + (erase-buffer) + (insert response) + 0))) + (defun kagi--call-fastgpt (prompt) "Submit the given PROMPT to the FastGPT API. @@ -219,7 +256,7 @@ interpretation." (all-flags (append call-process-flags curl-flags (list kagi-fastgpt-api-url))) - (return (apply #'call-process-region all-flags))) + (return (apply #'kagi--call-process-region all-flags))) (if (zerop return) (buffer-string) (error "Call to FastGPT API returned with status %s" return))))) @@ -239,7 +276,7 @@ interpretation." (all-flags (append call-process-flags curl-flags (list kagi-summarizer-api-url))) - (return (apply #'call-process-region all-flags))) + (return (apply #'kagi--call-process-region all-flags))) (if (zerop return) (buffer-string) (error "Call to Summarizer API returned with status %s" return)))))