;;; HTTP response
;;; Copyright (C) 2026 David Thompson <dave@spritely.institute>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

;;; Commentary:
;;;
;;; HTTP Response bindings.
;;;
;;; Code:

(define-module (web response)
  #:use-module (fibers streams)
  #:use-module (hoot ffi)
  #:export (response?
            response-body
            response-code))

;; Response -> i32
(define-foreign response-test
  "rt" "response_test" (ref extern) -> i32)
;; Response -> ReadableStream
(define-foreign %response-body
  "rt" "response_body" (ref extern) -> (ref extern))
;; Response -> i32
(define-foreign %response-code
  "rt" "response_code" (ref extern) -> i32)

(define (response? obj)
  "Return @code{#t} if @var{obj} is a response object."
  (and (external? obj) (eq? (response-test obj) 1)))

(define (response-body response)
  "Return the body of @var{response} as an open input port."
  (open-input-stream (%response-body response)))

(define (response-code response)
  "Return the HTTP status code of @var{response}"
  (%response-code response))
