diff options
author | Marius Bakke <mbakke@fastmail.com> | 2018-06-11 23:52:15 +0200 |
---|---|---|
committer | Marius Bakke <mbakke@fastmail.com> | 2018-06-11 23:52:15 +0200 |
commit | a032b4454b3fc67e11e9fc2d8c2345288065fa29 (patch) | |
tree | c208124b79dbd2224b68c52106aa72ff2ebfa7ab /guix/import/json.scm | |
parent | b5724230fed2d043206df20d12a45bb962b7ee77 (diff) | |
parent | 6321ce42ab4d9ab788d858cb19bde4aa7a0e3ecc (diff) | |
download | guix-patches-a032b4454b3fc67e11e9fc2d8c2345288065fa29.tar guix-patches-a032b4454b3fc67e11e9fc2d8c2345288065fa29.tar.gz |
Merge branch 'master' into staging
Diffstat (limited to 'guix/import/json.scm')
-rw-r--r-- | guix/import/json.scm | 24 |
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))) |