mirror of
https://git.savannah.gnu.org/git/guix.git
synced 2025-01-31 06:46:50 +01:00
import: github: Request API v3 in the 'Accept' header.
* guix/import/json.scm (json-fetch): Add #:headers argument and honor it. * guix/import/github.scm (latest-released-version): Pass #:headers to 'json-fetch'.
This commit is contained in:
parent
a50eed201b
commit
2766282f5a
2 changed files with 16 additions and 7 deletions
|
@ -1,6 +1,6 @@
|
||||||
;;; GNU Guix --- Functional package management for GNU
|
;;; GNU Guix --- Functional package management for GNU
|
||||||
;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
|
;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
|
||||||
;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
|
;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org>
|
||||||
;;;
|
;;;
|
||||||
;;; This file is part of GNU Guix.
|
;;; This file is part of GNU Guix.
|
||||||
;;;
|
;;;
|
||||||
|
@ -132,7 +132,12 @@ (define (latest-released-version url package-name)
|
||||||
(json (json-fetch
|
(json (json-fetch
|
||||||
(if token
|
(if token
|
||||||
(string-append api-url "?access_token=" token)
|
(string-append api-url "?access_token=" token)
|
||||||
api-url))))
|
api-url)
|
||||||
|
#:headers
|
||||||
|
;; Ask for version 3 of the API as suggested at
|
||||||
|
;; <https://developer.github.com/v3/>.
|
||||||
|
`((Accept . "application/vnd.github.v3+json")
|
||||||
|
(user-agent . "GNU Guile")))))
|
||||||
(if (eq? json #f)
|
(if (eq? json #f)
|
||||||
(if token
|
(if token
|
||||||
(error "Error downloading release information through the GitHub
|
(error "Error downloading release information through the GitHub
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
;;; GNU Guix --- Functional package management for GNU
|
;;; GNU Guix --- Functional package management for GNU
|
||||||
;;; Copyright © 2014 David Thompson <davet@gnu.org>
|
;;; Copyright © 2014 David Thompson <davet@gnu.org>
|
||||||
;;; Copyright © 2015, 2016 Eric Bavier <bavier@member.fsf.org>
|
;;; Copyright © 2015, 2016 Eric Bavier <bavier@member.fsf.org>
|
||||||
|
;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
|
||||||
;;;
|
;;;
|
||||||
;;; This file is part of GNU Guix.
|
;;; This file is part of GNU Guix.
|
||||||
;;;
|
;;;
|
||||||
|
@ -25,17 +26,20 @@ (define-module (guix import json)
|
||||||
#:export (json-fetch
|
#:export (json-fetch
|
||||||
json-fetch-alist))
|
json-fetch-alist))
|
||||||
|
|
||||||
(define (json-fetch url)
|
(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"))))
|
||||||
"Return a representation of the JSON resource URL (a list or hash table), or
|
"Return a representation of the JSON resource URL (a list or hash table), or
|
||||||
#f if URL returns 403 or 404."
|
#f if URL returns 403 or 404. HEADERS is a list of HTTP headers to pass in
|
||||||
|
the query."
|
||||||
(guard (c ((and (http-get-error? c)
|
(guard (c ((and (http-get-error? c)
|
||||||
(let ((error (http-get-error-code c)))
|
(let ((error (http-get-error-code c)))
|
||||||
(or (= 403 error)
|
(or (= 403 error)
|
||||||
(= 404 error))))
|
(= 404 error))))
|
||||||
#f))
|
#f))
|
||||||
;; Note: many websites returns 403 if we omit a 'User-Agent' header.
|
(let* ((port (http-fetch url #:headers headers))
|
||||||
(let* ((port (http-fetch url #:headers '((user-agent . "GNU Guile")
|
|
||||||
(Accept . "application/json"))))
|
|
||||||
(result (json->scm port)))
|
(result (json->scm port)))
|
||||||
(close-port port)
|
(close-port port)
|
||||||
result)))
|
result)))
|
||||||
|
|
Loading…
Reference in a new issue