oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
525 stars 42 forks source link

api list #130

Closed awb99 closed 2 years ago

awb99 commented 2 years ago

Thanks for the amazing martian library! Its really an amazing design. Rest apis on the first look seem to be all the same. But on a second look, they all have very small differences. This customization is not easy to get done. Martian solves it. Great design!

Now to my point: It would be great to have a list of publicly available pis that either bring open-api specs, or to have a resource where apis that dont support open-api are fully speced.

If we could gradually build a list of many apis, then it would be much easier to do rest-apis in clojure. I am pretty sure that there are already many of such speced apis out there. Perhaps we could add them to a wiki?

oliyh commented 2 years ago

Hello,

Thank you for your kind words! There are already lists like https://github.com/public-apis/public-apis so I wouldn't be surprised if someone had already done it, needs a bit of searching however. Probably makes sense as its own list, and I would be happy to link to it from martian documentation.

Cheers

awb99 commented 2 years ago

I have written a library that can be used to authorize apis that need oauth2 tokens. I am currently converting the rest apis to martian. I have implemented github google xero. I will post a link once I am done converting.

awb99 commented 2 years ago

Some comments after using martian for a few days:

  1. It is hard to find out if a rest api supports discovery with swagger/openid. This is not that something is mentioned in most api documents that i found.

  2. Many openid connect providers have discoverable apis. Are they the same as openid/swagger specs? Seems to be a differwnt standard.. But perhaps it is the same?

  3. When specing an api it would be nice to have a way to make requests when all specs are just s/Any and then infer the specs from actual api requests. I guess we could write an interceptor that does that.

  4. Is it possible to have the api specs in edn files? This would make them more extendable. Also we could create a spec from a discoverable api endpoint once and then save it to edn. This way it can be easily extended at runtime.

awb99 commented 2 years ago

rest apis that work with oauth2 and martian: https://github.com/pink-gorilla/webly/tree/master/oauth2/src/modular/rest/martian supports google, github, xero.

examples how to use the rest calls: https://github.com/pink-gorilla/webly/tree/master/demo-rest/src/rest

What I dont like so much: https://github.com/pink-gorilla/webly/blob/master/demo-rest/src/rest/google.clj google has multiple api endpoint roots. I did add 3 google related martian "connection" exports. They use the same oauth token store. But they just have different endpoint roots.

the xero web api uses two custom header interceptors (on top of the oauth token header). a tenant-id header, which I create once (sort of a company with which to work). This works niceyl. But then the query-api allows to search for changed data since a date. And for this interceptor I did not manage to use a field from the request call. https://github.com/pink-gorilla/webly/blob/master/oauth2/src/modular/rest/martian/xero.clj

awb99 commented 2 years ago

How can I rewrite this header-interceptor so that it sets the "If-Modified-Since" header based on a parameter :modified-since

 (->> (martian/response-for t :invoice-list-since 
                                   {:modified-since "2022-01-01T00:00:00"
                                    :where "(Type == \"ACCREC\")"       
                                    :page 1})
            :body
            :Invoices
            print-invoices)

(defn add-modified-since-header [dt]
  {:name ::add-modified-since-header
   :enter (fn [ctx]
            (assoc-in ctx
                      [:request :headers "If-Modified-Since"]
                      dt))})

 {:route-name :invoice-list-since
    :summary "list invoices modified-since"
    :method :get
    :path-parts ["/api.xro/2.0/Invoices/"]
    :query-schema {s/Any s/Any}
    :produces ["application/json"]
    :consumes ["application/json"]
    :interceptors [(add-modified-since-header "2022-01-01T00:00:00")]}
awb99 commented 2 years ago

Any idea on my interceptor issue? Thanks

oliyh commented 2 years ago

Hi,

The ctx contains a key :params which contain the parameters passed to the martian call. You can use that to find your :modified-since value.

e.g.

(def add-modified-since-header
  {:name ::add-modified-since-header
   :enter (fn [ctx]
            (assoc-in ctx
                      [:request :headers "If-Modified-Since"]
                      (get-in ctx [:params :modified-since])))})

We have strayed a bit from the original issue topic of api lists, I will close this issue but feel free to open another one to deal with a particular topic, thanks for your feedback.

Cheers