Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python3/.env.template → .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions curl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cookie_jar.txt
13 changes: 13 additions & 0 deletions curl/README.md
Original file line number Diff line number Diff line change
@@ -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
```
54 changes: 54 additions & 0 deletions curl/team-edition.sh
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions operations/auth.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query authLogin($token: String!) {
authLogin(provider: "token", credentials: { token: $token }) {
userTokens {
userId
}
authStatus
}
}
5 changes: 5 additions & 0 deletions operations/create_team.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query createTeam($teamId: ID!) {
createTeam(teamId: $teamId) {
teamId
}
}
3 changes: 3 additions & 0 deletions operations/delete_team.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query deleteTeam($teamId: ID!, $force: Boolean) {
deleteTeam(teamId: $teamId, force: $force)
}
4 changes: 2 additions & 2 deletions python3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions python3/te.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down