{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute WPS request without token" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base_url = 'http://demo-twitcher.cloud.dkrz.de/ows/proxy/emu'\n", "url = \"{}/ows/proxy/emu?service=WPS&version=1.0.0&request=Execute&identifier=hello&DataInputs=name=Stranger\".format(base_url)\n", "url" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "resp = requests.get(url)\n", "resp.ok" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'AccessForbidden' in resp.text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute WPS request with token" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get a token from keycloak via phoenix client: \n", "https://demo-phoenix.cloud.dkrz.de" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "access_token = ''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "headers = {'Authorization': 'Bearer {}'.format(access_token)}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "resp = requests.get(url, headers=headers)\n", "resp.ok" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'ProcessSucceeded' in resp.text" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'Hello Stranger' in resp.text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use Birdy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from birdy import WPSClient\n", "emu = WPSClient(url=base_url, headers=headers)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = emu.hello(name='Stranger')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response.get()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Keycloak client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "keycloak_url = 'https://auth-test.ceda.ac.uk'\n", "token_endpoint = '/auth/realms/master/protocol/openid-connect/token'\n", "client_id = 'demo-1'\n", "# copy secret from demo-1 client\n", "client_secret = ''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get OAuth access token from Keycloak" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "token_url = \"{}{}\".format(keycloak_url, token_endpoint)\n", "token_url" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from oauthlib.oauth2 import BackendApplicationClient\n", "from requests_oauthlib import OAuth2Session\n", "\n", "client = BackendApplicationClient(client_id=client_id)\n", "oauth = OAuth2Session(client=client)\n", "token = oauth.fetch_token(\n", " token_url,\n", " # scope='compute',\n", " client_id=client_id,\n", " client_secret=client_secret,\n", " include_client_id=True,\n", " verify=True)\n", "token" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "token['access_token']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run request with birdy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from birdy import WPSClient\n", "headers = {'Authorization': 'Bearer {}'.format(token['access_token'])}\n", "emu = WPSClient(url=base_url, headers=headers)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = emu.hello(name='Stranger')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response.get()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }