aboutsummaryrefslogtreecommitdiff
path: root/guix/import/json.scm
diff options
context:
space:
mode:
authorJelle Licht <jlicht@fsfe.org>2018-06-10 20:35:39 +0200
committerJelle Licht <jlicht@fsfe.org>2018-06-10 22:15:12 +0200
commit3edf0d53a4043c30f3ff87b3b4b7b47d1bac1397 (patch)
tree6b5d908ff681825c989f57b4eb69eecb2942bf0d /guix/import/json.scm
parent670a5e543033022a66d930d62a7dbff510f095db (diff)
downloadguix-3edf0d53a4043c30f3ff87b3b4b7b47d1bac1397.tar
guix-3edf0d53a4043c30f3ff87b3b4b7b47d1bac1397.tar.gz
import: json: Consolidate duplicate json-fetch functionality.
* guix/import/json.scm (json-fetch): Return a list or hash table. (json-fetch-alist): New procedure. * guix/import/github.scm (json-fetch*): Remove. (latest-released-version): Use json-fetch. * guix/import/cpan.scm (module->dist-name): Use json-fetch-alist. (cpan-fetch): Likewise. * guix/import/crate.scm (crate-fetch): Likewise. * guix/import/gem.scm (rubygems-fetch): Likewise. * guix/import/pypi.scm (pypi-fetch): Likewise. * guix/import/stackage.scm (stackage-lts-info-fetch): Likewise.
Diffstat (limited to 'guix/import/json.scm')
-rw-r--r--guix/import/json.scm24
1 files changed, 17 insertions, 7 deletions
diff --git a/guix/import/json.scm b/guix/import/json.scm
index c76bc9313c..3f2ab1e3ea 100644
--- a/guix/import/json.scm
+++ b/guix/import/json.scm
@@ -22,15 +22,25 @@
#:use-module (guix http-client)
#:use-module (guix import utils)
#:use-module (srfi srfi-34)
- #:export (json-fetch))
+ #:export (json-fetch
+ json-fetch-alist))
(define (json-fetch url)
- "Return an alist representation of the JSON resource URL, or #f on failure."
+ "Return a representation of the JSON resource URL (a list or hash table), or
+#f if URL returns 403 or 404."
(guard (c ((and (http-get-error? c)
- (= 404 (http-get-error-code c)))
- #f)) ;"expected" if package is unknown
- (let* ((port (http-fetch url #:headers '((user-agent . "GNU Guile")
- (Accept . "application/json"))))
- (result (hash-table->alist (json->scm port))))
+ (let ((error (http-get-error-code c)))
+ (or (= 403 error)
+ (= 404 error))))
+ #f))
+ ;; Note: many websites returns 403 if we omit a 'User-Agent' header.
+ (let* ((port (http-fetch url #:headers '((user-agent . "GNU Guile")
+ (Accept . "application/json"))))
+ (result (json->scm port)))
(close-port port)
result)))
+
+(define (json-fetch-alist url)
+ "Return an alist representation of the JSON resource URL, or #f if URL
+returns 403 or 404."
+ (hash-table->alist (json-fetch url)))