uri.scm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ;;; guile-openai --- An OpenAI API client for Guile
  2. ;;; Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
  3. ;;;
  4. ;;; This file is part of guile-openai.
  5. ;;;
  6. ;;; guile-openai is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU Affero General Public License as
  8. ;;; published by the Free Software Foundation, either version 3 of the
  9. ;;; License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-openai is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Affero General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Affero General Public
  17. ;;; License along with guile-openai. If not, see
  18. ;;; <https://www.gnu.org/licenses/>.
  19. (define-module (openai utils uri)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (web uri)
  22. #:export (->uri
  23. ->relative-ref
  24. join-uri-paths
  25. resolve-uri-refs))
  26. (define (->uri val)
  27. (or (cond ((uri? val) val)
  28. ((string? val) (string->uri val))
  29. (else #f))
  30. (error "Invalid uri:" val)))
  31. (define (->relative-ref val)
  32. (or (cond ((relative-ref? val) val)
  33. ((string? val) (string->relative-ref val))
  34. (else #f))
  35. (error "Invalid relative-ref:" val)))
  36. (define (join-uri-paths . paths)
  37. (string-append "/" (encode-and-join-uri-path
  38. (append-map split-and-decode-uri-path paths))))
  39. (define (resolve-uri-refs base . refs)
  40. (let* ((base (->uri base))
  41. (uris (cons base (map ->relative-ref refs)))
  42. (from-right (reverse uris)))
  43. (build-uri (uri-scheme base)
  44. #:userinfo (uri-userinfo base)
  45. #:host (uri-host base)
  46. #:port (uri-port base)
  47. #:path (apply join-uri-paths (map uri-path uris))
  48. #:query (any uri-query from-right)
  49. #:fragment (any uri-fragment from-right))))