Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base url #4

Open
yayitswei opened this issue Aug 1, 2015 · 4 comments
Open

base url #4

yayitswei opened this issue Aug 1, 2015 · 4 comments

Comments

@yayitswei
Copy link

Hi, thanks for making remote. Is there a way to add a base url to be used by all the endpoints in a service?

@jakemcc
Copy link
Contributor

jakemcc commented Aug 5, 2015

Hi @yayitswei. Glad you're enjoying the library.

I can't think of a way to do that automatically. In some of our client's that use remote we have ended up just using str to concatenate a base url to the rest of the route. Example below.

(r/defservice offer-service
  (r/headers {"Content-Type" "application/json"})
  (r/on-status
   (200 [res]
        (json/parse-string (:body res)))
   (201 [res]
        (json/parse-string (:body res)))
   (500 [_]
        (error/bomb "There was an error trying to fetch offers.")))

  (r/endpoint :create-offer (str config/nginx-endpoint "/offer_data/v2/offers")
              (r/method :post)
              (r/on-request [req body]
                            (s/validate OfferCreate body)
                            (-> req
                                (assoc :body (json/generate-string body))
                                (merge common-opts)))))

It isn't documented well but each of these macros are really just helper functions for manipulating a data structure. You could write your own functions to create the data structure that take into account a base url.

@kidpollo
Copy link

kidpollo commented Aug 5, 2015

I think what Jake says is accurate. This library was initially conceived as a CLJS client library so base paths where not in mind. Adding a helper that can set up a base path should not be difficult at https://github.com/outpace/remote/blob/master/src/outpace/remote/service.cljc#L144

Cheers

@iwillig
Copy link

iwillig commented Aug 7, 2015

My question is similar to to yayitswei. I have a base url and a token that I need to make configurable. This is for a service that requires the token to be in the header. I attempted to solve this with dynamic vars, but that does not work. My solution was something like this,

(def ^:dynamic token "")

(defmacro with-token [new-token & body]
  `(binding [token ~new-token]
     ~@body))

(defservice service
  (headers {"X-Service" token}) ...)

I am guessing this is failing because defservice is a marco but I am not sure.
Thoughts?

@nyoung-outpace
Copy link

@iwillig apologies for the late reply, I think the feature you're looking for to make something like this work is the on-request form.

The construction you have above will attempt to look up the value of token at service-definition time (when, I'm assuming the token is not actually set). I think that means you want the token to be looked up at request time (presumably the individual requests will be wrapped in a with-token form).

I believe that will look something like:

(defservice service
  (on-request [req] (assoc-in req [:headers "X-Service"] token)) ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants