Skip to content

Commit

Permalink
docs(api): add python
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Nov 19, 2024
1 parent 99a4e94 commit 58df013
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .web/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ export default defineConfig({
},
],
},
{
text: 'Python',
link: '/developers/api/python/',
},
{
text: 'Go',
link: '/developers/api/go/',
Expand Down
4 changes: 4 additions & 0 deletions .web/docs/developers/api/java/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ servers {
}
```

::: info Learn More
For more details on using gRPC with Java, check out the [gRPC Java Documentation](https://grpc.io/docs/languages/java/basics/).
:::

<style>
.tech-icon {
width: 32px;
Expand Down
148 changes: 148 additions & 0 deletions .web/docs/developers/api/python/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" class="tech-icon" alt="Python" /> Python Client

Gate provides a Python API for integrating with your Python applications. You can use the API to interact with Gate programmatically using either gRPC or HTTP protocols.

## Environment Setup

::: tip Best DX
We recommend using [uv](https://github.com/astral-sh/uv) for beginners and experienced developers alike!
:::

::: code-group

```bash [uv (Recommended)]
# Install uv (on macOS/Linux)
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create a new virtual environment
uv init
```

```bash [venv]
# Create a new virtual environment
python3 -m venv .venv

# Activate the environment (Unix)
source .venv/bin/activate

# Activate the environment (Windows)
.venv\Scripts\activate
```

```bash [poetry]
# Install poetry
curl -sSL https://install.python-poetry.org | python3 -

# Create new project
poetry init
poetry shell
```

```bash [pipenv]
# Install pipenv
pip install pipenv

# Create environment and activate shell
pipenv install
pipenv shell
```

:::

## Installation

::: code-group

```bash [uv (Recommended)]
uv add minekube-gate-grpc-python minekube-gate-protocolbuffers-python --index https://buf.build/gen/python
```

```bash [pip]
python3 -m pip install minekube-gate-grpc-python minekube-gate-protocolbuffers-python --extra-index-url https://buf.build/gen/python
```

```bash [poetry]
poetry add minekube-gate-grpc-python minekube-gate-protocolbuffers-python --source buf.build/gen/python
```

```bash [pipenv]
pipenv install minekube-gate-grpc-python minekube-gate-protocolbuffers-python --extra-index-url https://buf.build/gen/python
```

:::

## Usage Example

Here's a basic example of using the Gate Python API to connect to Gate and list servers:

::: code-group

```python [main.py]
<!--@include: ./main.py -->
```

:::

## Running the Example

1. Make sure Gate is running with the API enabled
2. Save one of the example scripts above to `main.py`
3. Run the script:

::: code-group

```bash [uv (Recommended)]
uv run main.py
{
"servers": [
{
"name": "server3",
"address": "localhost:25568"
},
{
"name": "server4",
"address": "localhost:25569"
},
{
"name": "server1",
"address": "localhost:25566"
},
{
"name": "server2",
"address": "localhost:25567"
}
]
}
```

```bash [python]
python3 main.py
```

```bash [poetry]
poetry run python main.py
```

```bash [pipenv]
pipenv run python main.py
```

:::

::: info Learn More

- [uv Documentation](https://github.com/astral-sh/uv) - Learn more about the recommended Python package manager
- [gRPC Python Documentation](https://grpc.io/docs/languages/python/basics/) - Learn more about using gRPC with Python
:::

<style>
.tech-icon {
width: 32px;
height: 32px;
display: inline-block;
vertical-align: middle;
margin-right: 12px;
position: relative;
top: -2px;
}
</style>
30 changes: 30 additions & 0 deletions .web/docs/developers/api/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import grpc
import json
from google.protobuf import json_format
from minekube.gate.v1 import gate_service_pb2 as gatepb
from minekube.gate.v1 import gate_service_pb2_grpc as gateapi


def main():
# Create a gRPC channel
channel = grpc.insecure_channel('localhost:8080')

# Create a stub (client)
stub = gateapi.GateServiceStub(channel)

try:
# List servers
response = stub.ListServers(gatepb.ListServersRequest())
# Convert to JSON and print
json_response = json_format.MessageToDict(response)
print(json.dumps(json_response, indent=2))

except grpc.RpcError as e:
print(f"RPC failed: {e}")

finally:
channel.close()


if __name__ == '__main__':
main()
13 changes: 13 additions & 0 deletions .web/docs/developers/api/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "python"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"minekube-gate-grpc-python>=1.68.0.1.20241118150055",
"minekube-gate-protocolbuffers-python>=28.3.0.2.20241118150055",
]

[[tool.uv.index]]
url = "https://buf.build/gen/python"
93 changes: 93 additions & 0 deletions .web/docs/developers/api/python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58df013

Please sign in to comment.