summaryrefslogtreecommitdiff
path: root/pog/swhid.scm
blob: e0cd3bdb822a7cc18ffec67d9d05fced02f609a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;;; Preservation of Guix
;;; Copyright © 2021 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Preservation of Guix.
;;;
;;; Preservation of Guix is free software: you can redistribute it
;;; and/or modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; Preservation of Guix is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
;;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Preservation of Guix.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (pog swhid)
  #:use-module (ice-9 match)
  #:use-module (json)
  #:use-module (pog errors)
  #:use-module (pog git)
  #:use-module (pog log)
  #:use-module (pog references)
  #:use-module (pog web)
  #:use-module (srfi srfi-2)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-43)
  #:use-module (rnrs bytevectors)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (web uri)
  #:export (%swh-api-url
            rate-limit?
            rate-limit-limit
            rate-limit-remaining
            rate-limit-reset
            rate-limit-interval
            swh-known
            reference->swhid))

(define %swh-api-url
  (make-parameter
   (string->uri "https://archive.softwareheritage.org/api/1")))

(define-record-type <rate-limit>
  (make-rate-limit limit remaining reset)
  rate-limit?
  (limit rate-limit-limit)
  (remaining rate-limit-remaining)
  (reset rate-limit-reset))

(define (response->rate-limit response)
  (and-let* ((headers (response-headers response))
             (limit (and=> (assq-ref headers 'x-ratelimit-limit)
                           string->number))
             (remaining (and=> (assq-ref headers 'x-ratelimit-remaining)
                               string->number))
             (reset (and=> (assq-ref headers 'x-ratelimit-reset)
                           string->number)))
    (make-rate-limit limit remaining reset)))

(define (wait-for-rate-limit-reset rate-limit)
  (let ((wait (min (abs (- (rate-limit-reset rate-limit) (current-time)))
                   3600)))
    (log-info "SWH quota reached; sleeping for ~a sec..." wait)
    (force-output (%pog-log-port))
    (sleep wait)))

(define (build-resolve-url swhid)
  (let* ((base (%swh-api-url))
         (path (encode-and-join-uri-path
                (append (split-and-decode-uri-path (uri-path base))
                        `("resolve" ,swhid)))))
    (build-uri (uri-scheme base)
               #:userinfo (uri-userinfo base)
               #:host (uri-host base)
               #:port (uri-port base)
               #:path (string-append "/" path "/"))))

(define (swhid-exists? swhid)
  (call-with-values (lambda () (http-head (build-resolve-url swhid)))
    (lambda (response body)
      (define rate-limit
        (and-let* ((headers (response-headers response))
                   (limit (and=> (assq-ref headers 'x-ratelimit-limit)
                                 string->number))
                   (remaining (and=> (assq-ref headers 'x-ratelimit-remaining)
                                     string->number))
                   (reset (and=> (assq-ref headers 'x-ratelimit-reset)
                                 string->number)))
          (make-rate-limit limit remaining reset)))
      (cond
       ((= (response-code response) 200) #t)
       ((= (response-code response) 404) #f)
       ((and (= (response-code response) 429)
             rate-limit
             (zero? (rate-limit-remaining rate-limit)))
        (let ((wait (min (abs (- (rate-limit-reset rate-limit)
                                 (current-time)))
                         3600)))
          (log-info "SWH quota reached; sleeping for ~a sec..." wait)
          (force-output (%pog-log-port))
          (sleep wait))
        (swhid-exists? swhid))
       (else (error "Could not check existence of SWHID" swhid response))))))

(define (json-string->known-result json)
  (map (match-lambda
         (((? string? swhid) . (("known" . (? boolean? value))))
          (cons swhid value)))
       (json-string->scm json)))

(define (build-known-url)
  (let* ((base (%swh-api-url))
         (path (encode-and-join-uri-path
                (append (split-and-decode-uri-path (uri-path base))
                        '("known")))))
    (build-uri (uri-scheme base)
               #:userinfo (uri-userinfo base)
               #:host (uri-host base)
               #:port (uri-port base)
               #:path (string-append "/" path "/"))))

(define (%swh-known swhids)
  "Determine whether each SWHID in SWHIDS is known to the archive.
This procedure returns an alist with keys from SWHIDS and Boolean
values indicating whether the given SWHID is known.  The length of
SWHIDS must not exceed 1000."
  (define headers
    '((content-type . (application/json))))

  (define swhids-json
    (match swhids
      (#(_ ...) (scm->json-string swhids))
      ((_ ...) (scm->json-string (list->vector swhids)))))

  (call-with-values (lambda () (http-post (build-known-url)
                                          #:body swhids-json
                                          #:headers headers
                                          #:decode-body? #f))
    (lambda (response body)
      (match (response-code response)
        (200 (json-string->known-result (utf8->string body)))
        (429 (or (and-let* ((rate-limit (pk (response->rate-limit response))))
                   (and (zero? (rate-limit-remaining rate-limit))
                        (begin (wait-for-rate-limit-reset rate-limit)
                               (%swh-known swhids))))
                 (error "Error occurred while calling /known" response body)))
        (_ (error "Error occurred while calling /known" response body))))))

(define (swh-known swhids)
  "Determine whether each SWHID in SWHIDS is known to the archive.
This procedure returns an alist with keys from SWHIDS and Boolean
values indicating whether the given SWHID is known."
  (match swhids
    (#(_ ...)
     (let loop ((k 0) (acc '()))
       (if (= k (vector-length swhids))
           acc
           (let* ((size (min (- (vector-length swhids) k) 1000))
                  (vec (make-vector size)))
             (vector-copy! vec 0 swhids k (+ k size))
             (loop (+ k size) (append (%swh-known vec) acc))))))
    ((_ ...)
     (swh-known (list->vector swhids)))))

(define* (reference->swhid ref #:optional digest)
  (match (reference-value ref)
    (('git-reference . _) (git-reference->swhid ref digest))
    ((or (? string?) ((? string?) ...)) (web-reference->swhid ref digest))
    (_ (pog-bail-error ref))))