<commit_before>#lang racket

(require (for-syntax racket/path racket/match racket/list json)
         scribble/base
         "main.rkt")

(provide require-gapi-doc
         require/gapi-doc
         api-key
         (all-from-out "main.rkt"))

(begin-for-syntax

 (define (gen-racket js stx)
   #`(begin
       (require json net/url net/uri-codec)
       #,@(append*
           (for/list ([(k v) (hash-ref js 'resources)])
             (for/list ([(k v) (hash-ref v 'methods)])
               (do-method stx js k v))))))
 
  (define (do-method stx root mn mv)
    (define name (string->symbol
                  (regexp-replace* #rx"\\." (hash-ref mv 'id) "-")))
    (define api-param-names (hash-keys (hash-ref root 'parameters)))
    (define params (hash-ref mv 'parameters (hash)))
    (define (required? x)
      (and (hash-has-key? x 'required)
           (hash-ref x 'required)))
    (define req-params (for/hasheq ([(k v) params]
                                    #:when (required? v))
                         (values k v)))
    (define opt-params (for/hasheq ([(k v) params]
                                    #:when (not (required? v)))
                         (values k v)))
    (define req-param-names (hash-keys req-params))
    (define opt-param-names (hash-keys opt-params))
    (define _body-params
      (hash-ref (hash-ref (hash-ref root 'schemas)
                          (string->symbol
                           (hash-ref (hash-ref mv 'request (hash)) '$ref ""))
                          (hash))
                'properties
                (hash)))
    ;; The "licensing" API has a problem where it duplicates a
    ;; required parameter (such as "userID" or "productID") in the
    ;; body parameters. Filter such problems here.
    (define body-params
      (for/hasheq ([(k v) _body-params]
                   #:when (not (hash-has-key? req-params k)))
        (values k v)))
    (define body-param-names (hash-keys body-params))
    (define all-opt-param-names (append opt-param-names
                                        body-param-names
                                        api-param-names))
    (define qps (append req-param-names opt-param-names api-param-names))
    (define symbol->keyword (compose1 string->keyword symbol->string))
    #`(define (#,(datum->syntax stx name)
               #,@(append* (map (lambda (x)
                                  (list (symbol->keyword x)
                                        x))
                                req-param-names))
               #,@(append* (map (lambda (x)
                                  (list (symbol->keyword x)
                                        (list x
                                              (cond [(eq? x 'key)
                                                     #'(api-key)]
                                                    [else ''NONE]))))
                                all-opt-param-names)))
        (define base-uri #,(hash-ref root 'baseUrl))
        (define res-path #,(hash-ref mv 'path))
        (define _qpstr (alist->form-urlencoded
                        (filter-map
                         (lambda (k v)
                           (cond [(eq? v 'NONE) #f]
                                 [else (cons (string->symbol k) v)]))
                         (list #,@(map symbol->string qps))
                         (list #,@qps))))
        (define qpstr (cond [(equal? _qpstr "") ""]
                            [else (string-append "?" _qpstr)]))
        (define url (string->url (string-append base-uri res-path qpstr)))
        (define h (list "Content-Type: application/json"))
        (define body
          (jsexpr->bytes
           (for/hasheq ([k (list #,@(map symbol->string body-param-names))]
                        [v (list #,@body-param-names)]
                        #:when (not (eq? v 'NONE)))
             (values (string->symbol k) v)))) 
        (define in
          #,(match (hash-ref mv 'httpMethod)
              ["GET" #'(get-pure-port url h)]
              ["POST" #'(post-pure-port url body h)]
              ["PUT" #'(put-pure-port url body h)]<commit_msg>Change not-supplied default value to 'N/A<commit_after>#lang racket

(require (for-syntax racket/path racket/match racket/list json)
         scribble/base
         "main.rkt")

(provide require-gapi-doc
         require/gapi-doc
         api-key
         (all-from-out "main.rkt"))

(begin-for-syntax

 (define (gen-racket js stx)
   #`(begin
       (require json net/url net/uri-codec)
       #,@(append*
           (for/list ([(k v) (hash-ref js 'resources)])
             (for/list ([(k v) (hash-ref v 'methods)])
               (do-method stx js k v))))))
 
  (define (do-method stx root mn mv)
    (define name (string->symbol
                  (regexp-replace* #rx"\\." (hash-ref mv 'id) "-")))
    (define api-param-names (hash-keys (hash-ref root 'parameters)))
    (define params (hash-ref mv 'parameters (hash)))
    (define (required? x)
      (and (hash-has-key? x 'required)
           (hash-ref x 'required)))
    (define req-params (for/hasheq ([(k v) params]
                                    #:when (required? v))
                         (values k v)))
    (define opt-params (for/hasheq ([(k v) params]
                                    #:when (not (required? v)))
                         (values k v)))
    (define req-param-names (hash-keys req-params))
    (define opt-param-names (hash-keys opt-params))
    (define _body-params
      (hash-ref (hash-ref (hash-ref root 'schemas)
                          (string->symbol
                           (hash-ref (hash-ref mv 'request (hash)) '$ref ""))
                          (hash))
                'properties
                (hash)))
    ;; The "licensing" API has a problem where it duplicates a
    ;; required parameter (such as "userID" or "productID") in the
    ;; body parameters. Filter such problems here.
    (define body-params
      (for/hasheq ([(k v) _body-params]
                   #:when (not (hash-has-key? req-params k)))
        (values k v)))
    (define body-param-names (hash-keys body-params))
    (define all-opt-param-names (append opt-param-names
                                        body-param-names
                                        api-param-names))
    (define qps (append req-param-names opt-param-names api-param-names))
    (define symbol->keyword (compose1 string->keyword symbol->string))
    #`(define (#,(datum->syntax stx name)
               #,@(append* (map (lambda (x)
                                  (list (symbol->keyword x)
                                        x))
                                req-param-names))
               #,@(append* (map (lambda (x)
                                  (list (symbol->keyword x)
                                        (list x
                                              (cond [(eq? x 'key)
                                                     #'(api-key)]
                                                    [else ''N/A]))))
                                all-opt-param-names)))
        (define base-uri #,(hash-ref root 'baseUrl))
        (define res-path #,(hash-ref mv 'path))
        (define _qpstr (alist->form-urlencoded
                        (filter-map
                         (lambda (k v)
                           (cond [(eq? v 'N/A) #f]
                                 [else (cons (string->symbol k) v)]))
                         (list #,@(map symbol->string qps))
                         (list #,@qps))))
        (define qpstr (cond [(equal? _qpstr "") ""]
                            [else (string-append "?" _qpstr)]))
        (define url (string->url (string-append base-uri res-path qpstr)))
        (define h (list "Content-Type: application/json"))
        (define body
          (jsexpr->bytes
           (for/hasheq ([k (list #,@(map symbol->string body-param-names))]
                        [v (list #,@body-param-names)]
                        #:when (not (eq? v 'N/A)))
             (values (string->symbol k) v)))) 
        (define in
          #,(match (hash-ref mv 'httpMethod)
              ["GET" #'(get-pure-port url h)]
              ["POST" #'(post-pure-port url body h)]
              ["PUT" #'(put-pure-port url body h)]
{'fraction_non_alphanumeric': 0.14049087172046912, 'fraction_numerical': 0.00024180872929512756, 'mean_word_length': 1.2920476586311997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1288597', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2265, 'n_words': 632}