ProxyHandler proxies APIs

ProxyHandler passes requests to another URL and returns its response. Here are some reasons to use it:

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

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.

Once logged in, you can:

Reverse ProxyHandler

This configuration mirrors https://gramener.com/demo/ at demo/, but only allows authenticated users.

url:
    proxyhandler/gramener.com:
        pattern: /$YAMLURL/(demo|uistatic|node_modules|bowerlib)/(.*)
        handler: ProxyHandler
        kwargs:
            url: https://gramener.com/{0}/{1}
            auth: true
        cache:
            expiry: {duration: 300}

All requests to the URLs demo, uistatic, node_modules, bowerlib 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.