diff --git a/python3/.env.template b/.env.template similarity index 94% rename from python3/.env.template rename to .env.template index a8e817b..9ce514e 100644 --- a/python3/.env.template +++ b/.env.template @@ -5,6 +5,6 @@ api_token= # See `serverURL` field here: https://github.com/dbeaver/cloudbeaver/wiki/Server-configuration server_url= -# `api` by dafault. +# `api` by default. # See `serviceURI` field here: https://github.com/dbeaver/cloudbeaver/wiki/Server-configuration service_uri=api diff --git a/README.md b/README.md index 91a1634..146a01f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,12 @@ This repo contains examples of using GraphQL API for [CloudBeaver Enterprise](https://dbeaver.com/cloudbeaver-enterprise/), [CloudBeaver AWS](https://aws.amazon.com/marketplace/pp/prodview-kijugxnqada5i), and [DBeaver Team Edition](https://dbeaver.com/dbeaver-team-edition/). -The repo layout is self-explanatory: the folder [go](go) contains examples for the Go programming language, [python3](python3) includes examples for Python 3, and so on. +# The repo layout + +- The [curl](curl) folder contains examples using `curl` command line tool. +- The [go](go) folder contains examples for the Go programming language. +- The [operations](operations) folder contains raw examples. They are used by projects from other folders. +- The [python3](python3) includes examples for Python 3. ## GraphQL API and prerequsites diff --git a/curl/.gitignore b/curl/.gitignore new file mode 100644 index 0000000..442513e --- /dev/null +++ b/curl/.gitignore @@ -0,0 +1 @@ +cookie_jar.txt diff --git a/curl/README.md b/curl/README.md new file mode 100644 index 0000000..8f7d2f2 --- /dev/null +++ b/curl/README.md @@ -0,0 +1,13 @@ +# Usage of DBeaver Team Edition GraphQL API with `curl` + +How to start: +1. Change the current directory to the directory with this README file. +2. Create the `../.env` file from the `../.env.template` (see the repository root for file `.env.template`) +```sh +cp ../.env.template ../.env +``` +3. Fill the environment variables in the `.env` file. +4. Execute the script with +```sh +./team-edition.sh +``` diff --git a/curl/team-edition.sh b/curl/team-edition.sh new file mode 100755 index 0000000..a3e0264 --- /dev/null +++ b/curl/team-edition.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env sh + +set -e + +script_dir="$(realpath "$(dirname "$0")")" + +cookie_jar="$script_dir/cookie_jar.txt" +touch "$cookie_jar" + +execute_gql() { + data="{ + \"query\": \"$2\", + \"variables\": { $3 } +}" + curl \ + --request 'POST' \ + --header 'Content-Type: application/json' \ + --data "$data" \ + --cookie "$cookie_jar" \ + --cookie-jar "$cookie_jar" \ + "$1" +} + +. "$script_dir"/../.env +# shellcheck disable=SC2154 +# These variables are set in the .env file +gql_endpoint="$server_url/$service_uri/gql" + +execute_cb_gql() { + execute_gql "$gql_endpoint" "$1" "$2" +} + +read_operation() { + sed 's/"/\\"/g' < "$script_dir"/../operations/"$1".gql +} + +auth() { + # shellcheck disable=SC2154 + # api_token is set in the .env file + execute_cb_gql "$(sed 's/"/\\"/g' < "$script_dir"/../operations/auth.gql)" "\"token\": \"$api_token\"" +} + +create_team() { + execute_cb_gql "$(read_operation create_team)" "\"teamId\": \"$1\"" +} + +delete_team() { + execute_cb_gql "$(read_operation delete_team)" "\"teamId\": \"$1\", \"force\": true" +} + +auth +team_id="cloudbeaver-graphql-examples_curl-team" +create_team "$team_id" +delete_team "$team_id" diff --git a/operations/auth.gql b/operations/auth.gql new file mode 100644 index 0000000..1420cef --- /dev/null +++ b/operations/auth.gql @@ -0,0 +1,8 @@ +query authLogin($token: String!) { + authLogin(provider: "token", credentials: { token: $token }) { + userTokens { + userId + } + authStatus + } +} diff --git a/operations/create_team.gql b/operations/create_team.gql new file mode 100644 index 0000000..5988ebd --- /dev/null +++ b/operations/create_team.gql @@ -0,0 +1,5 @@ +query createTeam($teamId: ID!) { + createTeam(teamId: $teamId) { + teamId + } +} diff --git a/operations/delete_team.gql b/operations/delete_team.gql new file mode 100644 index 0000000..b641999 --- /dev/null +++ b/operations/delete_team.gql @@ -0,0 +1,3 @@ +query deleteTeam($teamId: ID!, $force: Boolean) { + deleteTeam(teamId: $teamId, force: $force) +} diff --git a/python3/README.md b/python3/README.md index 7ef4a53..4fdadb8 100644 --- a/python3/README.md +++ b/python3/README.md @@ -19,9 +19,9 @@ python3 -m venv .venv ```sh pip install -r requirements.txt ``` -5. Create the `.env` file from the `.env.template` +5. Create the `../.env` file from the `../.env.template` (see the repository root for file `.env.template`) ```sh -cp .env.template .env +cp ../.env.template ../.env ``` 6. Fill the environment variables in the `.env` file. 7. Execute the script with diff --git a/python3/te.py b/python3/te.py index d4584eb..7b35fc6 100644 --- a/python3/te.py +++ b/python3/te.py @@ -1,12 +1,16 @@ import os + +from dotenv import load_dotenv from gql import gql, Client from gql.transport.aiohttp import AIOHTTPTransport -from dotenv import load_dotenv # Load important variables from the .env file. Among them there is an API token. # Consider using a more robust and secure approach when using this in production! # Also, see `get_api_token` function -load_dotenv() +script_dir = os.path.dirname(os.path.abspath(__file__)) +parent_dir = os.path.dirname(script_dir) +dotenv_path = os.path.join(parent_dir, ".env") +load_dotenv(dotenv_path=dotenv_path) def non_none_env(var_name: str) -> str: value = os.getenv(var_name)