.gitignorecommits | blame
ChangeLogcommits | blame
README.mdcommits | blame
build.cljcommits | blame
deps.edncommits | blame

# gemini.core

[![Clojars Project](](

A Clojure library to make Gemini requests that exposes some low-level
API to handle network requests.

## Usage

Import the library for e.g. with:

user=> (require '[gemini.core :as gemini])

### Documentation

`fetch` makes a Gemini request and returns a map with `:request`,
`:meta`, `:code` and `:body` as keys, or `:error` if an error occur.

The request needs to be closed afterwards using `close`.

user=> (gemini/fetch "gemini://")
 #object[com.omarpolo.gemini.Request 0x3b270767 "com.omarpolo.gemini.Request@3b270767"],
 :meta "gemini://",
 :code 31,
 #object[ 0x49358b66 ""]}

`body-as-string!` reads all the response into a string and returns it.
It also closes the request automatically.

user=> (-> (gemini/fetch "gemini://")
"# Project Gemini\n\n## Overview\n\nGemini is a new internet protocol which..."

`close` closes a request.  It needs to be called after every
(successful) request.

user=> (let [req (gemini/fetch "...")]
         (when-not (:error req)
           ;; do something with req
           (gemini/close req)))

`with-request` is a macro like `with-open` to making connection
easily.  It automatically closes the request and evaluates the body
only when the request is successful, otherwise throws an exception.

user=> (gemini/with-request [req (gemini/fetch "gemini://")]

## Streaming content

The `:body` keyword in the returned map is an instance of a Java
BufferedReader, so streaming content is easy.

However, `body-as-string!` needs to materialise the full reply, so in
case of a streaming request it will never return!

## text/gemini

This library only implements the network part of Gemini, it doesn't
try to handle any kind of content.  To handle text/gemini you can use
e.g. the [gemtext][gemtext] library:

user=> (require '[gemtext.core :as gemtext])
user=> (gemini/with-request [req (gemini/fetch "gemini://")]
         (gemtext/parse (:body req)))
[[:header-1 "Project Gemini"]
 [:text ""]
 [:header-2 "Overview"]
 [:text ""]
 [:text "Gemini is a new internet protocol which:"]

The [gemtext][gemtext] library supports streaming via the
`gemtext.core/parse` transducer:

user=> (gemini/with-request [req (gemini/fetch "gemini://")]
         (transduce gemtext/parser conj [] (line-seq (:body req))))