Discussion:
middleware to change url before request
David Smith
2015-03-01 15:11:08 UTC
Permalink
I have some middleware that I want to inspect the url of the request and
change it under certain circumstances. To demonstrate this I have the
following code:

(require '[aleph.http :as http]
'[aleph.http.client-middleware :as mw])

(defn alter-url-middleware
[client]
(fn [req]
(client (merge req (mw/parse-url "http://www.yahoo.com")))))
@(http/get "http://www.google.com" {:middleware alter-url-middleware})


However this code returns the response for google, not for yahoo. What am
I doing wrong?
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Zach Tellman
2015-03-01 16:36:57 UTC
Permalink
You'll also need to dissoc the :url, which shadows the other params.
Post by David Smith
(require '[aleph.http :as http]
'[aleph.http.client-middleware :as mw])
(defn alter-url-middleware
[client]
(fn [req]
(client (merge req (mw/parse-url "http://www.yahoo.com")))))
@(http/get "http://www.google.com" {:middleware alter-url-middleware})
However this code returns the response for google, not for yahoo. What am I doing wrong?
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Smith
2015-03-01 17:48:21 UTC
Permalink
I tried this but with no luck:

(defn alter-url-middleware
[client]
(fn [req]
(client (-> req
(merge (mw/parse-url "http://www.yahoo.com"))
(dissoc :url)))))
Post by Zach Tellman
You'll also need to dissoc the :url, which shadows the other params.
I have some middleware that I want to inspect the url of the request and
change it under certain circumstances. To demonstrate this I have the
(require '[aleph.http :as http]
'[aleph.http.client-middleware :as mw])
(defn alter-url-middleware
[client]
(fn [req]
(client (merge req (mw/parse-url "http://www.yahoo.com")))))
@(http/get "http://www.google.com" {:middleware alter-url-middleware})
However this code returns the response for google, not for yahoo. What am I doing wrong?
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Zach Tellman
2015-03-01 20:28:10 UTC
Permalink
Okay, the issue here was that the middleware was being applied after the
connection had already been opened, so it would be sent to the original
host regardless. I've checked in a change that moves the middleware
outside that scope, and have verified that everything works as expected.
Post by David Smith
(defn alter-url-middleware
[client]
(fn [req]
(client (-> req
(merge (mw/parse-url "http://www.yahoo.com"))
(dissoc :url)))))
Post by Zach Tellman
You'll also need to dissoc the :url, which shadows the other params.
I have some middleware that I want to inspect the url of the request and
change it under certain circumstances. To demonstrate this I have the
(require '[aleph.http :as http]
'[aleph.http.client-middleware :as mw])
(defn alter-url-middleware
[client]
(fn [req]
(client (merge req (mw/parse-url "http://www.yahoo.com")))))
@(http/get "http://www.google.com" {:middleware alter-url-middleware})
However this code returns the response for google, not for yahoo. What am I doing wrong?
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Smith
2015-03-02 15:42:50 UTC
Permalink
Great, this has enabled my stuff to work now. Aleph is really becoming
nice to use and middlewares are a really nice way to compose functionality
for something like a crawler.
Post by Zach Tellman
Okay, the issue here was that the middleware was being applied after the
connection had already been opened, so it would be sent to the original
host regardless. I've checked in a change that moves the middleware
outside that scope, and have verified that everything works as expected.
Post by David Smith
(defn alter-url-middleware
[client]
(fn [req]
(client (-> req
(merge (mw/parse-url "http://www.yahoo.com"))
(dissoc :url)))))
Post by Zach Tellman
You'll also need to dissoc the :url, which shadows the other params.
I have some middleware that I want to inspect the url of the request and
change it under certain circumstances. To demonstrate this I have the
(require '[aleph.http :as http]
'[aleph.http.client-middleware :as mw])
(defn alter-url-middleware
[client]
(fn [req]
(client (merge req (mw/parse-url "http://www.yahoo.com")))))
@(http/get "http://www.google.com" {:middleware alter-url-middleware})
However this code returns the response for google, not for yahoo. What
am I doing wrong?
--
You received this message because you are subscribed to the Google
Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...