ProxyHandler passes requests to another URL and returns its response. You can use it to:
For example:
url:
proxyhandler/github:
pattern: /$YAMLURL/github/(.*) # Send all requests for github/
handler: ProxyHandler
kwargs:
url: https://api.github.com/{0} # {0} is replaced with what's in (.*)
request_headers:
User-Agent: true # also send the user's browser info
This forwards github/search/users to https://api.github.com/search/users
. For example:
For more operators see the Github API
ProxyHandler configuration accepts these kwargs:
url
: URL endpoint to forward to. All captured groups in the pattern:
regular expression like /(group|item)/(.*)
can be used in the url as {0}
,
{1}
, etc. The first item (group|item)
becomes {0}
. The second is {1}
.request_headers
: dict of HTTP headers to be passed to the url.true
forwards this header from the request as-is. For “:User-Agent: true
passes the User-Agent header as-isCookie: true
passes the HTTP cookie header as-ishandler
as a variable. For example:
Authorization: 'Bearer {handler.session[google_access_token]}'
will
use the google_access_token
key from the session."*": true
all HTTP headers from the request as-isdefault
: dict of default URL query parameters. For example, alt: json
will
add a ?alt=json
to all requests by default. Over-ride this by requesting
?alt=something-else
explicitly.headers
: dict of HTTP headers to set on the responsemethods
: list of HTTP methods allowed. Defaults to GET
, HEAD
and POST
.
Remember that POST
and other write methods require XSRF – either a
?_xsrf=
argument or a X-Xsrftoken
HTTP header.prepare
: an expression or pipeline that accepts any of handler
and request
(a HTTPRequest) and modifies the request
in-placemodify
: an expression or pipeline that accepts any of handler
, request
(a
HTTPRequest), and response
(a HTTPResponse)
and modifies the response
in-placeconnect_timeout
: timeout for initial connection in seconds (default: 20)request_timeout
: timeout for entire request in seconds (default: 20)All standard handler kwargs like auth:
, cache:
,
xsrf_cookies
, error:
, etc. are also available.
NOTE: As of v1.62, websockets are also supported.
This configuration proxies the Google APIs at googleapis.com
:
url:
proxyhandler/google:
pattern: /$YAMLURL/google/(.*)
handler: ProxyHandler
kwargs:
url: https://www.googleapis.com/{0}
request_headers:
Authorization: "Bearer {handler.session[google_access_token]}"
To access the Google APIs, set up a Google Auth handler.
To get permission from the user for specific apps, add a scopes:
section that
lists the permissions you need. Here is the
full list of scopes.
Log into Google
Once logged in, you can:
https://www.google.com/m8/feeds/contacts/
as the endpoint
Note: Use the Gramex translate functionality to cache & edit translations.
You can set up a secret key to access the Google Translate API:
proxyhandler/googletranslate:
pattern: /$YAMLURL/googletranslate
handler: ProxyHandler
kwargs:
url: https://translation.googleapis.com/language/translate/v2
default:
# Get key from https://cloud.google.com/translate/docs/quickstart
key: ...
Now you can translate across languages:
To access the Google Custom Search API set up a custom search engine that searches the entire web. Then with an API key, use the REST API and reference, fetch search results.
proxyhandler/googlesearch:
pattern: /$YAMLURL/googlesearch
handler: ProxyHandler
kwargs:
url: https://www.googleapis.com/customsearch/v1
default:
key: ... # Your API key
cx: ... # Your custom search engine ID
Here are some examples of searches:
You can also analyze text using NLP:
proxyhandler/googlelanguage:
pattern: /$YAMLURL/googlelanguage/(.*)
handler: ProxyHandler
kwargs:
url: https://language.googleapis.com/{0}
method: POST
request_headers:
"*": true
default:
key: ...
Sending a POST request to googlelanguage/v1/documents:analyzeEntities
with
this JSON content analyzes the entities:
{
"document": {
"type": "PLAIN_TEXT",
"language": "en",
"content": "The Taj Mahal is in Agra"
},
"encodingType": "UTF8"
}
Click the button above to see the result
To analyze the sentiment of text, send a POST request to
googlelanguage/v1/documents:analyzeSentiment
with this JSON content:
{
"document": {
"type": "PLAIN_TEXT",
"language": "en",
"content": "Disliking watercraft is not really my thing"
},
"encodingType": "UTF8"
}
Click the button above to see the result
This configuration proxies the Facebook Graph API at graph.facebook.com
:
url:
proxyhandler/facebook:
pattern: /$YAMLURL/facebook/(.*)
handler: ProxyHandler
kwargs:
url: https://graph.facebook.com/{0}
default:
access_token: "{handler.session[user][access_token]}"
To access the Facebook APIs, set up a Facebook Auth handler.
Log into Facebook
Once logged in, you can:
This configuration mirrors https://gramener.com/gramex/ at gramex/, but only allows authenticated users.
url:
proxyhandler/gramener.com:
pattern: /$YAMLURL/(gramex/.*|ui/.*|node_modules/.*|style.css|common.js|img-2019|)
handler: ProxyHandler
kwargs:
url: https://gramener.com/{0}
auth: true
cache:
expiry: { duration: 300 }
All requests to the URLs gramex/, ui/, node_modules/*, etc are redirected
to gramener.com/
- but only if the user is logged in. This lets us expose
internal applications to users who are logged in via Gramex.
Further, it caches the response for 300s (5 min) – making this an authenticated caching reverse proxy.
Proxied Gramex page
NOTE: the page won’t display perfectly unless all URLs are fully mapped.
To modify the arguments before forwarding the request, use prepare:
. For example, this adds an
url:
replace:
pattern: /$YAMLURL/replace
handler: FormHandler
kwargs:
url: $YAMLPATH/flags.csv
prepare: request.headers.set('APIKey', 'My-API-Key')
This prepare:
expression or pipeline replaces the ?c=
with ?Cross=
. So
replace?c=Yes is actually the same as
flags?Cross=Yes.
You can use these variables in the prepare:
expression:
handler
: ProxyHandler instancerequest
: HTTPRequest objectresponse
: HTTPResponse objectYou can modify args
in-place and return None, or return a value that replaces args
.
Some sample uses:
handler.current_user
inside the prepare:
expression