ProxyHandler proxies APIs

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

ProxyHandler configuration accepts these kwargs:

All standard handler kwargs like auth:, cache:, xsrf_cookies, error:, etc. are also available.

NOTE: As of v1.62, websockets are also supported.

Google ProxyHandler

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.

Google Services

Log into Google

Once logged in, you can:

Google Translate

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:

Google Cloud NLP

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

Facebook ProxyHandler

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:

Proxy sites

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.

ProxyHandler prepare

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:

You can modify args in-place and return None, or return a value that replaces args.

Some sample uses:

ProxyHandler modify