From 2167cd27ce3972c42a4a044ae9a464086f8235fc Mon Sep 17 00:00:00 2001 From: Nikita Akilov <26031301+n5a5@users.noreply.github.com> Date: Wed, 14 May 2025 23:00:40 +0200 Subject: [PATCH 1/4] dbeaver/pro#5697 curl auth example --- curl/.gitignore | 1 + curl/team-edition.sh | 39 +++++++++++++++++++++++++++++++++++++++ operations/auth.gql | 8 ++++++++ 3 files changed, 48 insertions(+) create mode 100644 curl/.gitignore create mode 100755 curl/team-edition.sh create mode 100644 operations/auth.gql 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/team-edition.sh b/curl/team-edition.sh new file mode 100755 index 0000000..59631ec --- /dev/null +++ b/curl/team-edition.sh @@ -0,0 +1,39 @@ +#!/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"/../python3/.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" +} + +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\"" +} + +auth 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 + } +} From f61d4ca47eb6ce0df3d9de0bc5b959dbd4913f41 Mon Sep 17 00:00:00 2001 From: Nikita Akilov <26031301+n5a5@users.noreply.github.com> Date: Wed, 14 May 2025 23:24:55 +0200 Subject: [PATCH 2/4] dbeaver/pro#5697 create/delete team examples for curl --- curl/team-edition.sh | 15 +++++++++++++++ operations/create_team.gql | 5 +++++ operations/delete_team.gql | 3 +++ 3 files changed, 23 insertions(+) create mode 100644 operations/create_team.gql create mode 100644 operations/delete_team.gql diff --git a/curl/team-edition.sh b/curl/team-edition.sh index 59631ec..fa03205 100755 --- a/curl/team-edition.sh +++ b/curl/team-edition.sh @@ -30,10 +30,25 @@ 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/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) +} From 4039456c2de2463d57f97d6a61bd754f1fe8728d Mon Sep 17 00:00:00 2001 From: Nikita Akilov <26031301+n5a5@users.noreply.github.com> Date: Wed, 14 May 2025 23:46:14 +0200 Subject: [PATCH 3/4] dbeaver/pro#5697 move the .env file and its template to the root of the repo --- python3/.env.template => .env.template | 2 +- curl/team-edition.sh | 2 +- python3/te.py | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) rename python3/.env.template => .env.template (94%) 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/curl/team-edition.sh b/curl/team-edition.sh index fa03205..a3e0264 100755 --- a/curl/team-edition.sh +++ b/curl/team-edition.sh @@ -21,7 +21,7 @@ execute_gql() { "$1" } -. "$script_dir"/../python3/.env +. "$script_dir"/../.env # shellcheck disable=SC2154 # These variables are set in the .env file gql_endpoint="$server_url/$service_uri/gql" 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) From c9ea299a51bc1335018a5b0444847eeec6c2c4c1 Mon Sep 17 00:00:00 2001 From: Nikita Akilov <26031301+n5a5@users.noreply.github.com> Date: Thu, 15 May 2025 00:11:51 +0200 Subject: [PATCH 4/4] dbeaver/pro#5697 new curl readme & fixes to the old ones --- README.md | 7 ++++++- curl/README.md | 13 +++++++++++++ python3/README.md | 4 ++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 curl/README.md 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/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/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