@devkong/cli-nx 0.0.67-alpha.3 → 0.0.67-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/package.json +1 -1
  2. package/src/generators/preset/files/kotlin/.dockerignore +6 -0
  3. package/src/generators/preset/files/kotlin/.editorconfig +24 -0
  4. package/src/generators/preset/files/kotlin/.vscode/extensions.json +18 -0
  5. package/src/generators/preset/files/kotlin/.vscode/launch.json +16 -0
  6. package/src/generators/preset/files/kotlin/.vscode/settings.json +7 -0
  7. package/src/generators/preset/files/kotlin/.vscode/tasks.json +81 -0
  8. package/src/generators/preset/files/kotlin/Dockerfile +58 -0
  9. package/src/generators/preset/files/kotlin/README.md +172 -0
  10. package/src/generators/preset/files/kotlin/build.gradle.kts.template +64 -0
  11. package/src/generators/preset/files/kotlin/gradle/wrapper/gradle-wrapper.jar +0 -0
  12. package/src/generators/preset/files/kotlin/gradle/wrapper/gradle-wrapper.properties +6 -0
  13. package/src/generators/preset/files/kotlin/gradle.properties +25 -0
  14. package/src/generators/preset/files/kotlin/gradlew +245 -0
  15. package/src/generators/preset/files/kotlin/gradlew.bat +92 -0
  16. package/src/generators/preset/files/kotlin/kong.json.template +6 -0
  17. package/src/generators/preset/files/kotlin/project.json.template +29 -0
  18. package/src/generators/preset/files/kotlin/settings.gradle.kts +14 -0
  19. package/src/generators/preset/files/kotlin/src/main/kotlin/io/kong/extension/InputData.kt +26 -0
  20. package/src/generators/preset/files/kotlin/src/main/kotlin/io/kong/extension/Main.kt +83 -0
  21. package/src/generators/preset/files/kotlin/src/main/kotlin/io/kong/extension/OutputData.kt +15 -0
  22. package/src/generators/preset/files/kotlin/src/main/kotlin/io/kong/extension/RegisterFrontend.kt +28 -0
  23. package/src/generators/preset/files/kotlin/src/main/resources/META-INF/resources/index.html +362 -0
  24. package/src/generators/preset/files/kotlin/src/main/resources/application.properties.template +3 -0
  25. package/src/generators/preset/files/kotlin/src/main/resources/reflect-config.json.template +1 -0
  26. package/src/generators/preset/files/kotlin/src/test/kotlin/io/kong/extension/Test.kt +58 -0
  27. package/src/generators/preset/files/python/.dockerignore +5 -0
  28. package/src/generators/preset/files/python/.editorconfig +16 -0
  29. package/src/generators/preset/files/python/.gitattributes +25 -0
  30. package/src/generators/preset/files/python/.gitignore.template +69 -0
  31. package/src/generators/preset/files/python/.idea/inspectionProfiles/Project_Default.xml +7 -0
  32. package/src/generators/preset/files/python/.idea/inspectionProfiles/profiles_settings.xml +6 -0
  33. package/src/generators/preset/files/python/.idea/kong-cli-python-template.iml +10 -0
  34. package/src/generators/preset/files/python/.pylintrc +543 -0
  35. package/src/generators/preset/files/python/.vscode/extensions.json +14 -0
  36. package/src/generators/preset/files/python/.vscode/launch.json +19 -0
  37. package/src/generators/preset/files/python/.vscode/settings.json +65 -0
  38. package/src/generators/preset/files/python/Dockerfile +30 -0
  39. package/src/generators/preset/files/python/README.md +262 -0
  40. package/src/generators/preset/files/python/kong.json.template +6 -0
  41. package/src/generators/preset/files/python/project.json.template +8 -0
  42. package/src/generators/preset/files/python/pyproject.toml +3 -0
  43. package/src/generators/preset/files/python/requirements-dev.txt +10 -0
  44. package/src/generators/preset/files/python/requirements.txt +5 -0
  45. package/src/generators/preset/files/python/src/__init__.py +0 -0
  46. package/src/generators/preset/files/python/src/frontend.py +3 -0
  47. package/src/generators/preset/files/python/src/input_data.py +13 -0
  48. package/src/generators/preset/files/python/src/main.py +28 -0
  49. package/src/generators/preset/files/python/src/main_test.py +19 -0
  50. package/src/generators/preset/files/python/src/output_data.py +15 -0
  51. package/src/generators/preset/files/python-with-s3/src/frontend.py +6 -0
  52. package/src/generators/preset/files/python-with-s3/src/input_data.py +17 -0
  53. package/src/generators/preset/files/python-with-s3/src/main.py +45 -0
  54. package/src/generators/preset/files/python-with-s3/src/main_test.py +36 -0
  55. package/src/generators/preset/files/python-with-s3/src/output_data.py +8 -0
  56. package/src/generators/preset/files/python-with-s3/src/s3.py +27 -0
  57. package/src/generators/preset/files/python-with-secret/src/frontend.py +17 -0
  58. package/src/generators/preset/files/python-with-secret/src/input_data.py +40 -0
  59. package/src/generators/preset/files/python-with-secret/src/main.py +51 -0
  60. package/src/generators/preset/files/python-with-secret/src/main_test.py +39 -0
  61. package/src/generators/preset/files/python-with-secret/src/output_data.py +16 -0
  62. package/src/generators/preset/generator.js +10 -0
  63. package/src/generators/preset/generator.js.map +1 -1
  64. package/src/generators/preset/schema.d.ts +6 -1
  65. package/src/generators/preset/schema.json +32 -2
@@ -0,0 +1,262 @@
1
+ # Kong Extension
2
+
3
+ Welcome to the Kong extension development workspace!
4
+
5
+ To have a better experience, open this file in preview mode of your IDE.
6
+
7
+ ### Prerequisites
8
+
9
+ Before you start, make sure you have the following installed depending on your OS:
10
+
11
+ [▶ Windows](#windows-setup)
12
+
13
+ [▶ Ubuntu](#ubuntu-setup)
14
+
15
+ [▶ macOS](#macos-setup)
16
+
17
+ To verify the installation and check the versions, run the following commands:
18
+
19
+ ```shell script
20
+ node --version
21
+ docker info
22
+ ```
23
+
24
+ ### Getting Started
25
+
26
+ 1. Make sure you're in the terminal and inside the extension folder you generated
27
+ 2. Run the following commands in your terminal:
28
+
29
+ ```shell script
30
+ # configure the base Kong URL (e.g., https://sandbox.kong.dev.app-dts.net)
31
+ kong configure
32
+
33
+ # install Python dependencies from requirements.txt
34
+ kong install --verbose
35
+ ```
36
+
37
+ ### Publish a New Version
38
+
39
+ To automatically tag your code with an incremented version number (e.g., 1, 2, 3), run:
40
+
41
+ ```shell script
42
+ kong publish-version --verbose
43
+ ```
44
+
45
+ ### List Available Versions
46
+
47
+ To see all available versions of your extension, run:
48
+
49
+ ```shell script
50
+ kong list-versions
51
+ ```
52
+
53
+ ### Assign an Alias to a Version
54
+
55
+ You can assign an alias (e.g., stable, beta) to a specific version. This alias will appear in the UI as a separate step in the workflow. For example, to assign the alias stable to version 1, use:
56
+
57
+ ```shell script
58
+ # send all traffic to version 1
59
+ kong set-alias stable 1
60
+
61
+ # split live traffic: 80% to version 1, 20% to version 2 (weights must add up to 100)
62
+ kong set-alias stable 1:80 2:20
63
+
64
+ # same split, plus mirror traffic to version 3 as a shadow (it serves no live traffic)
65
+ kong set-alias stable 1:80 2:20 3:shadow
66
+ ```
67
+
68
+ ### Using a Custom Profile
69
+
70
+ If you're developing an extension that will be used in different regions (or need to work with a specific profile), you can create a new profile using `kong configure`. Then, include the `--profile <name>` parameter when running the publish-version and set-alias commands to use the desired profile.
71
+
72
+ ### Update CLI
73
+
74
+ To install the latest version of the CLI, run:
75
+
76
+ ```shell script
77
+ npm i -g @devkong/cli@latest
78
+ ```
79
+
80
+ ### Setup
81
+
82
+ <details id="windows-setup">
83
+ <summary><strong>Windows</strong></summary>
84
+
85
+ ### Install Windows Subsystem for Linux (WSL2)
86
+
87
+ 1. Open PowerShell as Administrator.
88
+ 2. Run the following command to install WSL and set WSL2 as the default version:
89
+ ```powershell
90
+ wsl --install
91
+ ```
92
+ 3. Restart your computer if prompted.
93
+ 4. After restarting, install a preferred Linux distribution (e.g., Ubuntu) by running:
94
+ ```powershell
95
+ wsl --install -d Ubuntu
96
+ ```
97
+
98
+ ### Install Docker within WSL2
99
+
100
+ 1. Download Docker Desktop for Windows from [Docker's official website](https://www.docker.com/products/docker-desktop).
101
+ 2. Run the installer and follow the setup instructions.
102
+ 3. During installation, ensure the "Use the WSL 2 based engine" option is selected.
103
+ 4. Once installed, open Docker Desktop, go to Settings > General, and enable "Use WSL 2 based engine" if it isn't already enabled.
104
+ 5. Confirm Docker is working in your WSL2 distribution (e.g., Ubuntu) by running:
105
+ ```bash
106
+ docker --version
107
+ ```
108
+
109
+ ### Install Node.js and npm on Windows Host
110
+
111
+ 1. Download the Node.js installer for Windows from the [official Node.js website](https://nodejs.org/).
112
+ 2. Run the installer, making sure to include the npm package manager during installation.
113
+ 3. Verify installation by opening a new Command Prompt and running:
114
+ ```cmd
115
+ node -v
116
+ npm -v
117
+ ```
118
+
119
+ ### Install Python on Windows Host
120
+
121
+ 1. Download the Python installer (version >= 3.10) for Windows from the [official Python website](https://www.python.org/downloads/windows/).
122
+ 2. Run the installer, making sure to check "Add Python to PATH" during setup.
123
+ 3. Verify installation by running:
124
+ ```cmd
125
+ python --version
126
+ pip --version
127
+ ```
128
+
129
+ ### Install Poetry on Windows Host
130
+
131
+ 1. Open Command Prompt or PowerShell.
132
+ 2. Run the following command:
133
+ ```powershell
134
+ (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
135
+ ```
136
+ 3. Add Poetry to your PATH:
137
+ ```powershell
138
+ $env:Path += ";$env:USERPROFILE\.poetry\bin"
139
+ ```
140
+ 4. Verify installation:
141
+ ```powershell
142
+ poetry --version
143
+ ```
144
+
145
+ </details>
146
+
147
+ <details id="ubuntu-setup">
148
+ <summary><strong>Ubuntu</strong></summary>
149
+
150
+ ### Install Docker
151
+
152
+ 1. Follow the installation instructions on [Docker's official website](https://docs.docker.com/engine/install/ubuntu/).
153
+ 2. Start Docker and enable it to start on boot:
154
+ ```bash
155
+ sudo systemctl start docker
156
+ sudo systemctl enable docker
157
+ ```
158
+
159
+ ### Install Node.js and npm
160
+
161
+ 1. Run the following commands:
162
+ ```bash
163
+ sudo apt update
164
+ sudo apt install -y nodejs npm
165
+ ```
166
+ 2. Verify installation:
167
+ ```bash
168
+ node -v
169
+ npm -v
170
+ ```
171
+
172
+ ### Install Python
173
+
174
+ 1. Run the following commands:
175
+ ```bash
176
+ sudo apt update
177
+ sudo apt install -y python3 python3-pip
178
+ ```
179
+ 2. Verify installation:
180
+ ```bash
181
+ python3 --version
182
+ pip3 --version
183
+ ```
184
+
185
+ ### Install Poetry
186
+
187
+ 1. Run the following command:
188
+ ```bash
189
+ curl -sSL https://install.python-poetry.org | python3 -
190
+ ```
191
+ 2. Add Poetry to your PATH:
192
+ ```bash
193
+ export PATH="$HOME/.local/bin:$PATH"
194
+ ```
195
+ 3. Reload the shell:
196
+ ```bash
197
+ source ~/.bashrc
198
+ ```
199
+ 4. Verify installation:
200
+ ```bash
201
+ poetry --version
202
+ ```
203
+
204
+ </details>
205
+
206
+ <details id="macos-setup">
207
+ <summary><strong>macOS</strong></summary>
208
+
209
+ To emulate a Linux platform, you need to install Rosetta:
210
+
211
+ ```shell script
212
+ softwareupdate --install-rosetta
213
+ ```
214
+
215
+ ### Install Docker
216
+
217
+ 1. Download and install Docker Desktop for Mac from [Docker's official website](https://www.docker.com/products/docker-desktop).
218
+
219
+ ### Install Node.js and npm
220
+
221
+ 1. Use Homebrew to install:
222
+ ```bash
223
+ brew install node
224
+ ```
225
+ 2. Verify installation:
226
+ ```bash
227
+ node -v
228
+ npm -v
229
+ ```
230
+
231
+ ### Install Python
232
+
233
+ 1. Use Homebrew to install:
234
+ ```bash
235
+ brew install python
236
+ ```
237
+ 2. Verify installation:
238
+ ```bash
239
+ python3 --version
240
+ pip3 --version
241
+ ```
242
+
243
+ ### Install Poetry
244
+
245
+ 1. Run the following command:
246
+ ```bash
247
+ curl -sSL https://install.python-poetry.org | python3 -
248
+ ```
249
+ 2. Add Poetry to your PATH:
250
+ ```bash
251
+ export PATH="$HOME/.local/bin:$PATH"
252
+ ```
253
+ 3. Reload the shell:
254
+ ```bash
255
+ source ~/.bashrc
256
+ ```
257
+ 4. Verify installation:
258
+ ```bash
259
+ poetry --version
260
+ ```
261
+
262
+ </details>
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "<%=id%>",
3
+ "name": "<%=name%>",
4
+ "sdk": "<%=platform%>",
5
+ "category": "unknown"
6
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "<%=name%>",
3
+ "$schema": "./node_modules/nx/schemas/project-schema.json",
4
+ "projectType": "application",
5
+ "sourceRoot": "./src",
6
+ "targets": {},
7
+ "tags": []
8
+ }
@@ -0,0 +1,3 @@
1
+ [tool.pytest.ini_options]
2
+ asyncio_mode = "auto"
3
+ asyncio_default_fixture_loop_scope = "function"
@@ -0,0 +1,10 @@
1
+ black==26.5.1
2
+ flake8==7.3.0
3
+ isort==8.0.1
4
+ pylint==4.0.6
5
+ pylint-pydantic==0.4.1
6
+ pytest==9.1.1
7
+ pytest-asyncio==1.4.0
8
+ pytest-cov==7.1.0
9
+ pytest-html==4.2.0
10
+ pytest-sugar==1.1.1
@@ -0,0 +1,5 @@
1
+ pydantic==2.13.4
2
+ kong-sdk==0.0.51
3
+ <% if (variant === "with-s3") { -%>
4
+ aiobotocore==2.15.2
5
+ <% } -%>
@@ -0,0 +1,3 @@
1
+ from kong.sdk.kong_frontend import KongFrontend, KongFrontendBuilder
2
+
3
+ frontend: KongFrontend = KongFrontendBuilder().build()
@@ -0,0 +1,13 @@
1
+ from pydantic import BaseModel, Field
2
+
3
+
4
+ class InputData(BaseModel):
5
+ a: int = Field(
6
+ examples=[1],
7
+ description="An integer number",
8
+ )
9
+
10
+ b: int = Field(
11
+ examples=[2],
12
+ description="An integer number",
13
+ )
@@ -0,0 +1,28 @@
1
+ import sys
2
+
3
+ from kong.sdk.app import App
4
+ from kong.sdk.kong_function import KongFunction
5
+ from kong.sdk.kong_function_context import KongFunctionContext
6
+
7
+ from frontend import frontend
8
+ from input_data import InputData
9
+ from output_data import OutputData
10
+
11
+
12
+ class Main(KongFunction):
13
+ async def handle(self, data: InputData, context: KongFunctionContext) -> OutputData:
14
+ return OutputData(
15
+ sum_of=data.a + data.b,
16
+ comment=f"sum of {data.a} and {data.b}",
17
+ )
18
+
19
+
20
+ app = App(
21
+ InputData,
22
+ OutputData,
23
+ Main,
24
+ frontend,
25
+ )
26
+
27
+ if __name__ == "__main__":
28
+ app.run(sys.argv)
@@ -0,0 +1,19 @@
1
+ from kong.sdk.conftest import ExtensionFixture, extension_test_context
2
+
3
+ from frontend import frontend
4
+ from input_data import InputData
5
+ from main import Main
6
+
7
+
8
+ async def test_should_return_value_from_string_field_when_input_is_provided(
9
+ create_extension: ExtensionFixture,
10
+ ):
11
+ extension = create_extension(Main, InputData)
12
+
13
+ input_data = InputData(
14
+ a=1,
15
+ b=2,
16
+ )
17
+
18
+ output = await extension.handle(input_data, extension_test_context(frontend))
19
+ assert output.sum_of == 3
@@ -0,0 +1,15 @@
1
+ from typing import Optional
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+
6
+ class OutputData(BaseModel):
7
+ sum_of: int = Field(
8
+ examples=[3],
9
+ description="Result of a + b",
10
+ )
11
+
12
+ comment: Optional[str] = Field(
13
+ examples=["This example extension returns sum of a and b"],
14
+ description="A string field",
15
+ )
@@ -0,0 +1,6 @@
1
+ from kong.sdk.kong_frontend import KongFrontend, KongFrontendBuilder
2
+ from kong.sdk.s3 import S3Secret
3
+
4
+ frontend: KongFrontend = (
5
+ KongFrontendBuilder().with_secret_types("s3", {"s3": S3Secret}).with_form().build()
6
+ )
@@ -0,0 +1,17 @@
1
+ from kong.sdk.kong_function_request import KongFunctionRequest
2
+ from pydantic import BaseModel, Field
3
+
4
+
5
+ class ExecuteData(BaseModel):
6
+ key: str = Field(
7
+ examples=["test/example.json"],
8
+ description="Key (path) of the object to download from the S3 bucket",
9
+ )
10
+
11
+
12
+ class InputData(BaseModel):
13
+ request: KongFunctionRequest
14
+
15
+ execute: ExecuteData = Field(
16
+ description="Parameters for the 'execute' operation",
17
+ )
@@ -0,0 +1,45 @@
1
+ import sys
2
+ from datetime import timedelta
3
+
4
+ from kong.sdk.app import App
5
+ from kong.sdk.kong_function import KongFunction
6
+ from kong.sdk.kong_function_context import KongFunctionContext
7
+ from kong.sdk.s3 import S3Secret
8
+
9
+ from frontend import frontend
10
+ from input_data import InputData
11
+ from output_data import OutputData
12
+ from s3 import create_s3_client
13
+
14
+
15
+ class Main(KongFunction):
16
+ async def handle(self, data: InputData, context: KongFunctionContext) -> OutputData:
17
+ secret: S3Secret = context.secret.get("s3").data
18
+ client = await context.state.use(
19
+ key=f"{secret.endpoint}:{secret.port}:{secret.bucket}:{secret.accessKey}",
20
+ create=lambda: create_s3_client(secret),
21
+ unused=(timedelta(minutes=5), lambda c: c.close()),
22
+ )
23
+
24
+ response = await client.get_object(
25
+ Bucket=secret.bucket,
26
+ Key=data.execute.key,
27
+ )
28
+
29
+ body = await response["Body"].read()
30
+ print("body size:", len(body))
31
+
32
+ return OutputData(
33
+ key=data.execute.key,
34
+ )
35
+
36
+
37
+ app = App(
38
+ InputData,
39
+ OutputData,
40
+ Main,
41
+ frontend,
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ app.run(sys.argv)
@@ -0,0 +1,36 @@
1
+ from frontend import frontend
2
+ from input_data import ExecuteData, InputData
3
+ from kong.sdk.conftest import ExtensionFixture, extension_test_context
4
+ from kong.sdk.kong_function_request import KongFunctionRequest
5
+ from kong.sdk.s3 import S3Secret
6
+ from main import Main
7
+
8
+ S3_SECRET = S3Secret(
9
+ accessKey="access",
10
+ secretKey="secret",
11
+ bucket="my-bucket",
12
+ region="us-east-1",
13
+ endpoint=None,
14
+ port=None,
15
+ useSSL=True,
16
+ pathStyle=False,
17
+ )
18
+
19
+
20
+ async def test_should_download_object_from_s3(
21
+ create_extension: ExtensionFixture,
22
+ ):
23
+ extension = create_extension(Main, InputData)
24
+
25
+ input_data = InputData(
26
+ request=KongFunctionRequest(operation="execute"),
27
+ execute=ExecuteData(key="test/example.json"),
28
+ )
29
+
30
+ context = extension_test_context(
31
+ frontend,
32
+ secrets={"s3": S3_SECRET},
33
+ )
34
+
35
+ output = await extension.handle(input_data, context)
36
+ assert output.key == "test/example.json"
@@ -0,0 +1,8 @@
1
+ from pydantic import BaseModel, Field
2
+
3
+
4
+ class OutputData(BaseModel):
5
+ key: str = Field(
6
+ examples=["test/example.json"],
7
+ description="Key of the object that was downloaded",
8
+ )
@@ -0,0 +1,27 @@
1
+ from aiobotocore.session import get_session
2
+ from botocore.config import Config
3
+ from kong.sdk.s3 import S3Secret
4
+
5
+
6
+ async def create_s3_client(secret: S3Secret):
7
+ # Standard AWS endpoints are derived by boto from the region; only S3-compatible
8
+ # stores (MinIO, Ceph, ...) need an explicit endpoint override.
9
+ endpoint_url = None
10
+ if secret.endpoint and "amazonaws.com" not in secret.endpoint:
11
+ scheme = "https" if secret.useSSL else "http"
12
+ endpoint_url = (
13
+ f"{scheme}://{secret.endpoint}:{secret.port}"
14
+ if secret.port
15
+ else f"{scheme}://{secret.endpoint}"
16
+ )
17
+
18
+ client_context = get_session().create_client(
19
+ "s3",
20
+ region_name=secret.region,
21
+ endpoint_url=endpoint_url,
22
+ use_ssl=secret.useSSL,
23
+ aws_access_key_id=secret.accessKey,
24
+ aws_secret_access_key=secret.secretKey,
25
+ config=(Config(s3={"addressing_style": "path"}) if secret.pathStyle else None),
26
+ )
27
+ return await client_context.__aenter__()
@@ -0,0 +1,17 @@
1
+ from kong.sdk.kong_frontend import KongFrontend, KongFrontendBuilder
2
+ from pydantic import BaseModel, Field
3
+
4
+
5
+ class MySecret(BaseModel):
6
+ my_key: str = Field(
7
+ examples=["example-0123456789"],
8
+ description="An example of secret field",
9
+ )
10
+
11
+
12
+ frontend: KongFrontend = (
13
+ KongFrontendBuilder()
14
+ .with_secret_types("primary", {"<%=name%>-secret": MySecret})
15
+ .with_form()
16
+ .build()
17
+ )
@@ -0,0 +1,40 @@
1
+ from typing import Optional
2
+
3
+ from kong.sdk.kong_function_request import KongFunctionRequest
4
+ from pydantic import BaseModel, Field
5
+
6
+
7
+ class OperationData(BaseModel):
8
+ a: int = Field(
9
+ examples=[1],
10
+ description="An integer number",
11
+ )
12
+
13
+ b: int = Field(
14
+ examples=[2],
15
+ description="An integer number",
16
+ )
17
+
18
+
19
+ class InputData(BaseModel):
20
+ request: KongFunctionRequest
21
+
22
+ sum: Optional[OperationData] = Field(
23
+ default=None,
24
+ description="Operands for the 'sum' operation",
25
+ )
26
+
27
+ sub: Optional[OperationData] = Field(
28
+ default=None,
29
+ description="Operands for the 'sub' operation",
30
+ )
31
+
32
+ multiply: Optional[OperationData] = Field(
33
+ default=None,
34
+ description="Operands for the 'multiply' operation",
35
+ )
36
+
37
+ divide: Optional[OperationData] = Field(
38
+ default=None,
39
+ description="Operands for the 'divide' operation",
40
+ )
@@ -0,0 +1,51 @@
1
+ import sys
2
+
3
+ from frontend import frontend
4
+ from input_data import InputData, OperationData
5
+ from kong.sdk.app import App
6
+ from kong.sdk.kong_function import KongFunction
7
+ from kong.sdk.kong_function_context import KongFunctionContext
8
+ from output_data import OutputData
9
+
10
+
11
+ class Main(KongFunction):
12
+ async def handle(self, data: InputData, context: KongFunctionContext) -> OutputData:
13
+ operation = data.request.operation
14
+ operands = getattr(data, operation, None)
15
+ if not isinstance(operands, OperationData):
16
+ raise ValueError(f"Missing or unsupported operation '{operation}'")
17
+
18
+ if operation == "sum":
19
+ result = operands.a + operands.b
20
+ elif operation == "sub":
21
+ result = operands.a - operands.b
22
+ elif operation == "multiply":
23
+ result = operands.a * operands.b
24
+ elif operation == "divide":
25
+ if operands.b == 0:
26
+ raise ValueError("Cannot divide by zero")
27
+ result = operands.a // operands.b
28
+ else:
29
+ raise ValueError(f"Unsupported operation '{operation}'")
30
+
31
+ comment = f"{operation} result is {result}"
32
+
33
+ secret = context.secret.find("primary")
34
+ if secret is not None:
35
+ comment += " (authenticated)"
36
+
37
+ return OutputData(
38
+ result=result,
39
+ comment=comment,
40
+ )
41
+
42
+
43
+ app = App(
44
+ InputData,
45
+ OutputData,
46
+ Main,
47
+ frontend,
48
+ )
49
+
50
+ if __name__ == "__main__":
51
+ app.run(sys.argv)
@@ -0,0 +1,39 @@
1
+ from frontend import MySecret, frontend
2
+ from input_data import InputData, OperationData
3
+ from kong.sdk.conftest import ExtensionFixture, extension_test_context
4
+ from kong.sdk.kong_function_request import KongFunctionRequest
5
+ from main import Main
6
+
7
+
8
+ async def test_should_add_operands_when_operation_is_sum(
9
+ create_extension: ExtensionFixture,
10
+ ):
11
+ extension = create_extension(Main, InputData)
12
+
13
+ input_data = InputData(
14
+ request=KongFunctionRequest(operation="sum"),
15
+ sum=OperationData(a=1, b=2),
16
+ )
17
+
18
+ output = await extension.handle(input_data, extension_test_context(frontend))
19
+ assert output.result == 3
20
+
21
+
22
+ async def test_should_note_authentication_when_secret_is_provided(
23
+ create_extension: ExtensionFixture,
24
+ ):
25
+ extension = create_extension(Main, InputData)
26
+
27
+ input_data = InputData(
28
+ request=KongFunctionRequest(operation="sum"),
29
+ sum=OperationData(a=1, b=2),
30
+ )
31
+
32
+ context = extension_test_context(
33
+ frontend,
34
+ {"primary": MySecret(my_key="example-0123456789")},
35
+ )
36
+ output = await extension.handle(input_data, context)
37
+
38
+ assert output.result == 3
39
+ assert output.comment == "sum result is 3 (authenticated)"