Liking cljdoc? Tell your friends :D

Martian Aleph plugin

This is a Clojure lib for a Martian plugin to use the Aleph http client.

Usage

For more details on how to use Martian, see the Martian docs.

In order to use Aleph as your http client, include the lib in your deps.edn:

{:deps {com.monkeyprojects/martian-aleph {:mvn/version "<VERSION>"}}}

Or when using Leiningen:

(...
 :dependencies [[com.monkeyprojects/martian-aleph "<VERSION>"]])

If the endpoint provides an OpenAPI definition, you can set up a context:

(require '[monkey.martian.aleph :as mma])

(def ctx (mma/bootstrap-openapi "https://some-rest-endpoint/swagger.json"))

After that, you can use martian.core/response-for to send requests to the remote API. If necessary, you can pass in additional options for the Martian requests and also to load the OpenAPI spec.

(mma/bootstrap-openapi
  "swagger-url"                                  ; Url to the swagger page
  {:interceptors my-interceptors}                ; Options to pass to the Martian client creator
  {:headers {"Authorization" "Bearer <token>"}}) ; Options to pass to the openapi request

The Martian options are merged in with the default-opts map, which already contain predefined interceptors.

When defining your own routes (when the other end does not provide a spec), you can use bootstrap:

(def routes
 [{:route-name ::test-route
   :path-parts ["/test"]
   :method :get
   :produces ["application/json"]}])

(def ctx (mma/bootstrap "http://remote-endpoint" routes))
;; You can also pass in additional options as a third argument

This will set up a Martian context that adds interceptors specific to using Aleph HTTP clients. Since Aleph always uses async requests, the response is a Manifold deferred, so you need to deref the response, or you can compose it with other async calls.

Testing

Martian provides functionality for mocking endpoints for testing purposes. Unfortunately, this is not client-agnostic, so we have provided custom functions for creating a Martian test context.

(require '[martian.test :as mt])
(require '[martian.core :as mc])

;; Create a test context
(def test-ctx (-> (mma/as-test-context ctx)
                  (mt/respond-with {::test-route {:status 200}})))

;; Testing
(is (= 200 (:status @(mc/response-for test-ctx ::test-route))))

In the future, it's the intention to actually PR this into the Martian source code itself, so we can also include the test code in martian.test itself. But until then, consider this a workaround.

License

Copyright (c) 2024 by Monkey Projects.

MIT License

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close