|
2 | 2 | import discohook
|
3 | 3 | import requests
|
4 | 4 |
|
5 |
| - |
6 | 5 | api_url = os.getenv('CHDB_API', 'https://chdb.fly.dev')
|
7 | 6 |
|
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] |
9 | 13 |
|
10 | 14 | 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) |
13 | 17 | ]
|
14 | 18 |
|
15 |
| -@discohook.command('query', 'Query chDB!', options = options) |
16 |
| -async def query_command(interaction, query, format): |
17 | 19 |
|
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! |
19 | 23 |
|
20 |
| - if not format: |
21 |
| - format = formats[-1] |
| 24 | + if not format: |
| 25 | + format = formats[-1] |
22 | 26 |
|
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) |
26 | 30 |
|
27 |
| - await interaction.followup(response.content.decode("utf-8")) |
| 31 | + await interaction.followup(response.content.decode("utf-8")) |
0 commit comments