Skip to content

Commit 8dac66d

Browse files
authored
Merge pull request #1 from laodouya/main
Fix query format
2 parents 91ca212 + e1a4b2d commit 8dac66d

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ---> Python
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*$py.class
5+
6+
# ---> IDE
7+
.vscode/*
8+
.idea/
9+
10+
# ---> venv
11+
venv/
12+
env/

src/commands/query.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22
import discohook
33
import requests
44

5-
65
api_url = os.getenv('CHDB_API', 'https://chdb.fly.dev')
76

8-
formats = ['CSV', 'CSVWithNames', 'TSV', 'TSVWithNames', 'JSONCompact']
7+
format_csv = discohook.Choice(name="csv", value="CSV")
8+
format_csv_name = discohook.Choice(name="csv_name", value="CSVWithNames")
9+
format_tsv = discohook.Choice(name="tsv", value="TSV")
10+
format_tsv_name = discohook.Choice(name="tsv_name", value="TSVWithNames")
11+
format_json = discohook.Choice(name="json", value="JSONCompact")
12+
formats = [format_csv, format_csv_name, format_tsv, format_tsv_name, format_json]
913

1014
options = [
11-
discohook.StringOption('query', 'Type your chdb SQL query here.', required = True),
12-
discohook.StringOption('format', 'Choose the response format.', choices = formats)
15+
discohook.StringOption('query', 'Type your chdb SQL query here.', required=True),
16+
discohook.StringOption('format', 'Choose the response format.', choices=formats)
1317
]
1418

15-
@discohook.command('query', 'Query chDB!', options = options)
16-
async def query_command(interaction, query, format):
1719

18-
await interaction.defer() # because it takes more than 3 seconds!
20+
@discohook.command('query', 'Query chDB!', options=options)
21+
async def query_command(interaction, query, format):
22+
await interaction.defer() # because it takes more than 3 seconds!
1923

20-
if not format:
21-
format = formats[-1]
24+
if not format:
25+
format = formats[-1]
2226

23-
params = { "default_format": format }
24-
headers = {"Content-Type":"text/plain;charset=UTF-8"}
25-
response = requests.post(api_url, params=params, data=query, headers=headers)
27+
params = {"default_format": format.value}
28+
headers = {"Content-Type": "text/plain;charset=UTF-8"}
29+
response = requests.post(api_url, params=params, data=query, headers=headers)
2630

27-
await interaction.followup(response.content.decode("utf-8"))
31+
await interaction.followup(response.content.decode("utf-8"))

0 commit comments

Comments
 (0)