# Flink SQL Gateway Python Client
A client library for accessing Flink SQL Gateway REST API
(mostly generated by [OpenAPI Generator](https://github.com/openapi-generators))
## Usage
First, create a client:
```python
from flink_gateway_api import Client
client = Client(base_url="http://localhost:8083")
```
If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead:
```python
from flink_gateway_api import AuthenticatedClient
client = AuthenticatedClient(base_url="http://localhost:80083", token="SuperSecretToken")
```
Now call your endpoint and use your models:
```python
import json
import time
from flink_gateway_api import Client
from flink_gateway_api.api.default import (
open_session,
close_session,
execute_statement,
fetch_results,
)
from flink_gateway_api.models import (
OpenSessionRequestBody,
ExecuteStatementResponseBody,
RowFormat,
)
with Client('http://localhost:8083') as client:
responses = open_session.sync(client=client, body=OpenSessionRequestBody.from_dict({
"properties": {
"idle-timeout": "10s"
},
"sessionName": "test_session"
}))
print(f"Open session response: {responses}")
select_result = execute_statement.sync(responses.session_handle, client=client,
body=ExecuteStatementResponseBody.from_dict({
"statement": "SELECT 23 as age, 'Alice Liddel' as name;",
}))
print(f"Select result: {select_result}")
time.sleep(1)
fetch_return = fetch_results.sync(
responses.session_handle,
select_result.operation_handle,
0,
client=client,
row_format=RowFormat.JSON,
)
print(f"Fetch return: {json.dumps(fetch_return.to_dict())}")
close_session.sync(responses.session_handle, client=client)
print(f"Session closed")
```
Or do the same thing with an async version:
```python
import json
import asyncio
from flink_gateway_api import Client
from flink_gateway_api.api.default import (
open_session,
close_session,
execute_statement,
fetch_results,
)
from flink_gateway_api.models import (
OpenSessionRequestBody,
ExecuteStatementResponseBody,
RowFormat,
)
async with Client('http://localhost:8083') as client:
responses = await open_session.asyncio(client=client, body=OpenSessionRequestBody.from_dict({
"properties": {
"idle-timeout": "10s"
},
"sessionName": "test_session"
}))
print(f"Open session response: {responses}")
select_result = await execute_statement.asyncio(responses.session_handle, client=client,
body=ExecuteStatementResponseBody.from_dict({
"statement": "SELECT 23 as age, 'Alice Liddel' as name;",
}))
print(f"Select result: {select_result}")
await asyncio.sleep(1) # Changed time.sleep to asyncio.sleep
fetch_return = await fetch_results.asyncio(
responses.session_handle,
select_result.operation_handle,
0,
client=client,
row_format=RowFormat.JSON,
)
print(f"Fetch return: {json.dumps(fetch_return.to_dict())}")
await close_session.asyncio(responses.session_handle, client=client)
print(f"Session closed")
```
The returned results will be like:
```json
{
"isQueryResult": true,
"jobID": "a0ad286b7259d4755327ce4969a8ec97",
"nextResultUri": "/v2/sessions/7625ad82-b23b-4118-9683-4a46b7c5022a/operations/353994b9-532d-4c0e-9258-688ec777f948/result/1?rowFormat=JSON",
"resultKind": "SUCCESS_WITH_CONTENT",
"resultType": "PAYLOAD",
"results": {
"columns": [
{
"name": "age",
"logicalType": {
"type": "INTEGER",
"nullable": false
},
"comment": null
},
{
"name": "name",
"logicalType": {
"type": "CHAR",
"nullable": false,
"length": 12
},
"comment": null
}
],
"columnInfos": [],
"data": [
{
"kind": "INSERT",
"fields": [
23,
"Alice Liddel"
]
}
],
"fieldGetters": [],
"rowFormat": "JSON"
}
}
```
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl="/path/to/certificate_bundle.pem",
)
```
You can also disable certificate validation altogether, but beware that **this is a security risk**.
```python
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl=False
)
```
Things to know:
1. Every path/method combo becomes a Python module with four functions:
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.
1. `asyncio`: Like `sync` but async instead of blocking
1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking
1. All path/query params, and bodies become method arguments.
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)
1. Any endpoint which did not have a tag will be in `flink_gateway_api.api.default`
## Advanced customizations
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case):
```python
from flink_gateway_api import Client
def log_request(request):
print(f"Request event hook: {request.method} {request.url} - Waiting for response")
def log_response(response):
request = response.request
print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
client = Client(
base_url="http://localhost:80083",
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
)
# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()
```
You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):
```python
import httpx
from flink_gateway_api import Client
client = Client(
base_url="http://localhost:80083",
)
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
client.set_httpx_client(httpx.Client(base_url="http://localhost:80083"))
```
# Developer
1. Quick start
```bash
# code gen
make py_118 # or
make py_119 # or
make py_120
# test
(
cd flink-sql-gateway-client && pytest tests
)
# check current version
cat flink-sql-gateway-client/pyproject.toml | grep version
version=$(cat flink-sql-gateway-client/pyproject.toml| grep version | cut -d '"' -f2)
# tag & release
git tag "release-$version"
git push origin "release-$version"
```
2. Release non-alpha versions
```bash
make py_119
# Manually edit flink-sql-gateway-client/pyproject.toml with desired version
version=$(cat flink-sql-gateway-client/pyproject.toml| grep version | cut -d '"' -f2)
git tag "release-$version"
git push origin "release-$version"
```
Raw data
{
"_id": null,
"home_page": "https://github.com/resink-ai/flink-sql-gateway-client",
"name": "flink-sql-gateway-api",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "Flink SQL Gateway, Python, Asyc Client",
"author": "Shijing Lu",
"author_email": "shijing.lu@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/14/69/4aa79539c8729c7f8277ed30854812d3b825870d42a797d231265a1e555b/flink_sql_gateway_api-1.20.0.tar.gz",
"platform": null,
"description": "# Flink SQL Gateway Python Client\nA client library for accessing Flink SQL Gateway REST API\n(mostly generated by [OpenAPI Generator](https://github.com/openapi-generators))\n\n## Usage\nFirst, create a client:\n\n```python\nfrom flink_gateway_api import Client\n\nclient = Client(base_url=\"http://localhost:8083\")\n```\n\nIf the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead:\n\n```python\nfrom flink_gateway_api import AuthenticatedClient\n\nclient = AuthenticatedClient(base_url=\"http://localhost:80083\", token=\"SuperSecretToken\")\n```\n\nNow call your endpoint and use your models:\n\n```python\nimport json\nimport time\nfrom flink_gateway_api import Client\nfrom flink_gateway_api.api.default import (\n open_session,\n close_session,\n execute_statement,\n fetch_results,\n)\nfrom flink_gateway_api.models import (\n OpenSessionRequestBody,\n ExecuteStatementResponseBody,\n RowFormat,\n)\n\nwith Client('http://localhost:8083') as client:\n responses = open_session.sync(client=client, body=OpenSessionRequestBody.from_dict({\n \"properties\": {\n \"idle-timeout\": \"10s\"\n },\n \"sessionName\": \"test_session\"\n }))\n print(f\"Open session response: {responses}\")\n\n select_result = execute_statement.sync(responses.session_handle, client=client,\n body=ExecuteStatementResponseBody.from_dict({\n \"statement\": \"SELECT 23 as age, 'Alice Liddel' as name;\",\n }))\n\n print(f\"Select result: {select_result}\")\n time.sleep(1)\n fetch_return = fetch_results.sync(\n responses.session_handle,\n select_result.operation_handle,\n 0,\n client=client,\n row_format=RowFormat.JSON,\n )\n print(f\"Fetch return: {json.dumps(fetch_return.to_dict())}\")\n\n close_session.sync(responses.session_handle, client=client)\n print(f\"Session closed\")\n```\n\nOr do the same thing with an async version:\n\n```python\nimport json\nimport asyncio\nfrom flink_gateway_api import Client\nfrom flink_gateway_api.api.default import (\n open_session,\n close_session,\n execute_statement,\n fetch_results,\n)\nfrom flink_gateway_api.models import (\n OpenSessionRequestBody,\n ExecuteStatementResponseBody,\n RowFormat,\n)\n\nasync with Client('http://localhost:8083') as client:\n responses = await open_session.asyncio(client=client, body=OpenSessionRequestBody.from_dict({\n \"properties\": {\n \"idle-timeout\": \"10s\"\n },\n \"sessionName\": \"test_session\"\n }))\n print(f\"Open session response: {responses}\")\n\n select_result = await execute_statement.asyncio(responses.session_handle, client=client,\n body=ExecuteStatementResponseBody.from_dict({\n \"statement\": \"SELECT 23 as age, 'Alice Liddel' as name;\",\n }))\n\n print(f\"Select result: {select_result}\")\n await asyncio.sleep(1) # Changed time.sleep to asyncio.sleep\n fetch_return = await fetch_results.asyncio(\n responses.session_handle,\n select_result.operation_handle,\n 0,\n client=client,\n row_format=RowFormat.JSON,\n )\n print(f\"Fetch return: {json.dumps(fetch_return.to_dict())}\")\n\n await close_session.asyncio(responses.session_handle, client=client)\n print(f\"Session closed\")\n```\n\nThe returned results will be like:\n```json\n{\n \"isQueryResult\": true,\n \"jobID\": \"a0ad286b7259d4755327ce4969a8ec97\",\n \"nextResultUri\": \"/v2/sessions/7625ad82-b23b-4118-9683-4a46b7c5022a/operations/353994b9-532d-4c0e-9258-688ec777f948/result/1?rowFormat=JSON\",\n \"resultKind\": \"SUCCESS_WITH_CONTENT\",\n \"resultType\": \"PAYLOAD\",\n \"results\": {\n \"columns\": [\n {\n \"name\": \"age\",\n \"logicalType\": {\n \"type\": \"INTEGER\",\n \"nullable\": false\n },\n \"comment\": null\n },\n {\n \"name\": \"name\",\n \"logicalType\": {\n \"type\": \"CHAR\",\n \"nullable\": false,\n \"length\": 12\n },\n \"comment\": null\n }\n ],\n \"columnInfos\": [],\n \"data\": [\n {\n \"kind\": \"INSERT\",\n \"fields\": [\n 23,\n \"Alice Liddel\"\n ]\n }\n ],\n \"fieldGetters\": [],\n \"rowFormat\": \"JSON\"\n }\n}\n```\n\nBy default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.\n\n```python\nclient = AuthenticatedClient(\n base_url=\"https://internal_api.example.com\", \n token=\"SuperSecretToken\",\n verify_ssl=\"/path/to/certificate_bundle.pem\",\n)\n```\n\nYou can also disable certificate validation altogether, but beware that **this is a security risk**.\n\n```python\nclient = AuthenticatedClient(\n base_url=\"https://internal_api.example.com\", \n token=\"SuperSecretToken\", \n verify_ssl=False\n)\n```\n\nThings to know:\n1. Every path/method combo becomes a Python module with four functions:\n 1. `sync`: Blocking request that returns parsed data (if successful) or `None`\n 1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.\n 1. `asyncio`: Like `sync` but async instead of blocking\n 1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking\n\n1. All path/query params, and bodies become method arguments.\n1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)\n1. Any endpoint which did not have a tag will be in `flink_gateway_api.api.default`\n\n## Advanced customizations\n\nThere are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case):\n\n```python\nfrom flink_gateway_api import Client\n\ndef log_request(request):\n print(f\"Request event hook: {request.method} {request.url} - Waiting for response\")\n\ndef log_response(response):\n request = response.request\n print(f\"Response event hook: {request.method} {request.url} - Status {response.status_code}\")\n\nclient = Client(\n base_url=\"http://localhost:80083\",\n httpx_args={\"event_hooks\": {\"request\": [log_request], \"response\": [log_response]}},\n)\n\n# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()\n```\n\nYou can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):\n\n```python\nimport httpx\nfrom flink_gateway_api import Client\n\nclient = Client(\n base_url=\"http://localhost:80083\",\n)\n# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.\nclient.set_httpx_client(httpx.Client(base_url=\"http://localhost:80083\"))\n```\n\n# Developer\n\n1. Quick start\n```bash\n# code gen\nmake py_118 # or \nmake py_119 # or\nmake py_120\n\n# test\n( \n cd flink-sql-gateway-client && pytest tests\n)\n\n# check current version\ncat flink-sql-gateway-client/pyproject.toml | grep version\nversion=$(cat flink-sql-gateway-client/pyproject.toml| grep version | cut -d '\"' -f2)\n\n# tag & release\ngit tag \"release-$version\"\ngit push origin \"release-$version\"\n\n```\n\n2. Release non-alpha versions\n\n```bash\nmake py_119 \n\n# Manually edit flink-sql-gateway-client/pyproject.toml with desired version\nversion=$(cat flink-sql-gateway-client/pyproject.toml| grep version | cut -d '\"' -f2)\ngit tag \"release-$version\"\ngit push origin \"release-$version\"\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A client library for accessing Flink SQL Gateway REST API",
"version": "1.20.0",
"project_urls": {
"Homepage": "https://github.com/resink-ai/flink-sql-gateway-client",
"Repository": "https://github.com/resink-ai/flink-sql-gateway-client"
},
"split_keywords": [
"flink sql gateway",
" python",
" asyc client"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8390f681b96f847be19d40efdd76d81fd5e2711cb06c8effc01fbdcd6430cc36",
"md5": "5bf0bc7faf495e41bf8d6babe1b4b410",
"sha256": "1eb3cecba82789d181a69d5cd122c748d40f61b32a2228a3b4a25daaee0f63df"
},
"downloads": -1,
"filename": "flink_sql_gateway_api-1.20.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5bf0bc7faf495e41bf8d6babe1b4b410",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 60227,
"upload_time": "2024-12-25T01:08:23",
"upload_time_iso_8601": "2024-12-25T01:08:23.940013Z",
"url": "https://files.pythonhosted.org/packages/83/90/f681b96f847be19d40efdd76d81fd5e2711cb06c8effc01fbdcd6430cc36/flink_sql_gateway_api-1.20.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "14694aa79539c8729c7f8277ed30854812d3b825870d42a797d231265a1e555b",
"md5": "f9dec7adad667c96b919838958a5e87d",
"sha256": "17b4ce1625566068312d43954b9cf3da9df730d8d0dc0fc19e3a8059cf5342bb"
},
"downloads": -1,
"filename": "flink_sql_gateway_api-1.20.0.tar.gz",
"has_sig": false,
"md5_digest": "f9dec7adad667c96b919838958a5e87d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 25975,
"upload_time": "2024-12-25T01:08:25",
"upload_time_iso_8601": "2024-12-25T01:08:25.309192Z",
"url": "https://files.pythonhosted.org/packages/14/69/4aa79539c8729c7f8277ed30854812d3b825870d42a797d231265a1e555b/flink_sql_gateway_api-1.20.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-25 01:08:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "resink-ai",
"github_project": "flink-sql-gateway-client",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "flink-sql-gateway-api"
}