diff mbox series

[bug#53818,v2,4/7] import: json: Make 'json-fetch' take '#:cached?' argument.

Message ID 92ca3095e8a5cf1197afd8883017c6b3729184f3.1644224421.git.public@yoctocell.xyz
State New
Headers show
Series Add Repology updater | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

Xinglu Chen Feb. 7, 2022, 9:07 a.m. UTC
* json.scm (json-fetch): Add ‘#:cached?’ keyword argument.
---
 guix/import/json.scm | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

Comments

M Feb. 7, 2022, 9:44 a.m. UTC | #1
Xinglu Chen schreef op ma 07-02-2022 om 10:07 [+0100]:
> +    (let* ((port   ((if cached?
> +                        http-fetch/cached
> +                        http-fetch)

Personally, instead of introdicing a cached? argument,
I would introduce a 'http-fetch' argument defaulting to 'http-fetch',
such that users of 'json-fetch' can set all the options of
'http-fetch/cached' (ttl, cache-miss ...).

(json-fetch #:http-fetch
            (lambda (uri . rest)
              (apply http-fetch/cached uri #:ttl foo #:cache-miss bar
                     rest)))

Greetings,
Maxime.
diff mbox series

Patch

diff --git a/guix/import/json.scm b/guix/import/json.scm
index 0c98bb25b8..6f88353659 100644
--- a/guix/import/json.scm
+++ b/guix/import/json.scm
@@ -3,6 +3,7 @@ 
 ;;; Copyright © 2015, 2016 Eric Bavier <bavier@member.fsf.org>
 ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
+;;; Copyright © 2022 Xinglu Chen <public@yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -37,17 +38,22 @@  (define-module (guix import json)
 (define* (json-fetch url
                      ;; Note: many websites returns 403 if we omit a
                      ;; 'User-Agent' header.
-                     #:key (headers `((user-agent . "GNU Guile")
-                                      (Accept . "application/json"))))
+                     #:key
+                     (headers `((user-agent . "GNU Guile")
+                                (Accept . "application/json")))
+                     cached?)
   "Return a representation of the JSON resource URL (a list or hash table), or
 #f if URL returns 403 or 404.  HEADERS is a list of HTTP headers to pass in
-the query."
+the query.  If CACHED? is #t, 'http-fetch/cached' will be used to fetch URL."
   (guard (c ((and (http-get-error? c)
                   (let ((error (http-get-error-code c)))
                     (or (= 403 error)
                         (= 404 error))))
              #f))
-    (let* ((port   (http-fetch url #:headers headers))
+    (let* ((port   ((if cached?
+                        http-fetch/cached
+                        http-fetch)
+                    url #:headers headers))
            (result (json->scm port)))
       (close-port port)
       result)))