Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add URL variables in AsyncAPI Document #1890

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c8e718d
Initial draft for variable urls
kakabisht Jul 3, 2023
fd59b5d
Peer feedback
kakabisht Jul 11, 2023
615a2f5
Introduce the image
kakabisht Jul 22, 2023
11dc90e
fix mermaid color
kakabisht Jul 31, 2023
43d954f
Fix Yaml code
kakabisht Aug 29, 2023
38d294a
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
ba3de47
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
81162b2
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
d397f87
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
5af9c19
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
f646c7c
Update pages/docs/concepts/asyncapi-document/_section.md
kakabisht Sep 4, 2023
428057b
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 4, 2023
a23505c
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 5, 2023
6ebaea5
Update pages/docs/concepts/asyncapi-document/variable-url.md
kakabisht Sep 5, 2023
a28c072
SME feedback
kakabisht Sep 14, 2023
c7ecca7
fix link
kakabisht Sep 14, 2023
157e45f
Partial SME feedback
kakabisht Oct 10, 2023
c62d99a
fix image
kakabisht Nov 16, 2023
e3a487a
fix a typo
kakabisht Nov 16, 2023
8ab024f
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
58280e4
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
94e814a
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
3064f17
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
462850a
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
fa4b6cc
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
35330ea
Update pages/docs/concepts/asyncapi document/variable-url.md
kakabisht Nov 27, 2023
8f23fe2
SME feedback except image
kakabisht Dec 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pages/docs/concepts/asyncapi-document/_section.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: AsyncAPI-document
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
weight: 0
---
94 changes: 94 additions & 0 deletions pages/docs/concepts/asyncapi-document/variable-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
title: Variables in URL
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
weight: 235
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
---

Event-driven architecture benefits from a defined base URL and rules for URL variables. You can easily manage multiple API endpoints, handling various server configurations and environments.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

URL variables are placeholders for values you can replace during runtime. Async API enables you to construct dynamic URLs with query parameters and additional information while enhancing the flexibility and maintainability of your API specifications.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

This article assumes that you have a basic understanding of AsyncAPI. For more information, refer to the [Event-Driven Architectures](../../tutorials/getting-started/event-driven-architectures.md).
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

## Add Dynamic Variables
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

To add variables to the URL using AsyncAPI between two servers. You can use the `server.url` and `components.serverVariables` fields to enable reusable variables across multiple servers.

The diagram below depicts how to construct dynamic URLs with query parameters.

```mermaid
graph LR
style A fill:#47BCEE, stroke:#333, stroke-width:2px;
style E fill:#47BCEE, stroke:#333, stroke-width:2px;
style C fill:#47BCEE, stroke:#333, stroke-width:2px;
style D fill:#47BCEE, stroke:#333, stroke-width:2px;
style B fill:#47BCEE, stroke:#333, stroke-width:2px;
style F fill:#47BCEE, stroke:#333, stroke-width:2px;

A[AsyncAPI Document]
B[Servers Section]
C[Base URLs]
D[components.serverVariables Section]
E[Domain Variable]
F[Port Variable]

A -->|contains| B
A -->|contains| D
B -->|includes| C
D -->|defines| E
D -->|defines| F
```
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

### Servers section:
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

Define the servers section in your AsyncAPI document, and include the base URLs for your API servers. Use placeholders enclosed in curly braces {} to represent the variables in the server URL. For example:

```yaml
servers:
production:
url: 'https://{domain}.example.com:{port}/v1'
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
description:
Production server staging: url: 'https://{domain}.example.com:{port}/v1'
description: Staging server
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
```

### ServerVariables section
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

Define the components.serverVariables section in your AsyncAPI document. For each variable used in the server URLs, provide a default value and an optional description:

```yaml
components:
serverVariables:
domain:
default: 'api'
description:
The domain of the API server port:
default: '8080'
description: The port of the API server
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
```

### Define domain and port variables

Both servers use the components.serverVariables definitions for the domain and port variables. To change the values of these variables, update their default values in the components.serverVariables section. Both servers' URLs will reflect the changes.

Here's the complete AsyncAPI document with the server URL variables:

```yaml
info:
title: Example API
version: '1.0.0'
servers:
production:
url: 'https://{domain}.example.com:{port}/v1'
description: Production server
staging:
url: 'https://{domain}.example.com:{port}/v1'
description: Staging server
components:
serverVariables:
domain:
default: 'api'
description: The domain of the API server
port:
default: '8080'
description: The port of the API server channels:# ...channel definitions...
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
```