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.
This commit is contained in:
Jelle Licht 2018-06-10 20:35:39 +02:00
parent 670a5e5430
commit 3edf0d53a4
No known key found for this signature in database
GPG key ID: DA4597F947B41025
7 changed files with 30 additions and 34 deletions

View file

@ -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)))