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

feat: add http adapter support for glee #320

Merged
merged 56 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
417fbd2
adding http header init
ritik307 Jun 26, 2022
a1d2778
resolved some requested changes
ritik307 Jul 10, 2022
52001a5
chore: updated the asyncapi.yml with title and desc. and made this.re…
Nov 4, 2022
978b87b
added .glee folder to gitignore and also updated .glee folder path
Nov 27, 2022
2bd2a42
updated asyncapi.yaml
Nov 27, 2022
b9b998c
added example example for http protocol
Dec 5, 2022
f4e3008
added example for http protocol
Dec 5, 2022
dff4daa
removed package-log from example
Jan 26, 2023
6e56ab0
resolved merge conflicts
Jan 26, 2023
32a611e
added client for http server
Jan 26, 2023
4f50fcc
added client and server example and adaptor for http
Jan 26, 2023
3903c62
removed code smell for duplication
Jan 26, 2023
13a75e3
removed code smell for duplication
Jan 26, 2023
b54b3a3
removed code smell for duplication
Jan 26, 2023
dd8e718
changed http client config
Jan 28, 2023
f77c719
added miltiple channel support for http server
Jan 30, 2023
2969974
added code to access the body
Jan 31, 2023
0b8b6ba
added body validation in spec and changed server function to handle d…
Jan 31, 2023
0290cf8
added multiple http server support
Feb 1, 2023
f3233c9
updated client code for http server
Feb 2, 2023
6946bc6
server will send error message when schema validation fails
Feb 4, 2023
ef0b5c0
made requested changes
Feb 6, 2023
d149908
added documentation regarding http server and client
Feb 6, 2023
46fcc21
added http client adapter
Feb 7, 2023
51f824e
fixed linting issue and code smell
Feb 8, 2023
5f998bd
added http event emitter
Feb 9, 2023
a43e80f
fixed linting issues
Feb 10, 2023
9982659
made changes in the client for http adaptor
Feb 11, 2023
194c333
test to check http client working
Feb 11, 2023
f343d50
updated http client
Feb 12, 2023
dd2957c
fixed linting issue
Feb 12, 2023
d79fd50
added authentication headers to client
Feb 17, 2023
a5ca06d
fixed code smell
Feb 17, 2023
948b6d3
made requested changes
Feb 18, 2023
2a52c5e
added emit message to http client controller
Feb 18, 2023
dd313ea
updated trendingAnimeRecieverController + fixed linting issue
Feb 18, 2023
6eeafc9
removed unnecessary commits
Feb 19, 2023
30d3924
removed unnecessary code
Feb 19, 2023
253c9e1
removed code smell
Feb 19, 2023
71f8097
added auth header in send function
Feb 19, 2023
77c4a45
Merge branch 'master' into add-http-headers
ritik307 Feb 24, 2023
18d0b4f
removed lint issues
Feb 27, 2023
69ca78a
Merge branch 'add-http-headers' of https://github.com/ritik307/glee i…
Feb 27, 2023
6895194
Merge branch 'master' into add-http-headers
ritik307 Feb 27, 2023
92bdbfe
made requested changes
Feb 28, 2023
c2f81d2
Merge branch 'add-http-headers' of https://github.com/ritik307/glee i…
Feb 28, 2023
e18d22e
changed request call from axios to got
Feb 28, 2023
0cd5dd2
changed auth token implementation in http client
Feb 28, 2023
af1d23a
made requested changes
Feb 28, 2023
31c44f0
made changes in Http server type
Feb 28, 2023
50cbba3
made requested changes
Mar 7, 2023
223ea80
made requested changes
Mar 8, 2023
4bfc92f
resolved code smell
Mar 8, 2023
5cc0755
made requested changes regaridng updating query data type
Mar 9, 2023
a4ff2a5
made changes regarding query param type in message
Mar 9, 2023
ae82f26
added types to missing query definations
Mar 11, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dist
coverage
.vscode
.glee
.next
.next
34 changes: 33 additions & 1 deletion docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default async function () {
kafka: {},
websocket: {},
mqtt: {},
cluster: {}
cluster: {},
http: {}
}
}

Expand Down Expand Up @@ -69,6 +70,24 @@ export default async function () {
}
}
}
},
http: {
server: {
httpServer: customServer, // A custom HTTP server of your own.
adapter: 'native',
port: process.env.PORT,
},
client: {
auth: {
token: process.env.TOKEN
},
query: {
foo: 'bar'
},
body: {
foo: 'bar'
}
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
}
Expand Down Expand Up @@ -137,6 +156,19 @@ These configurations apply to Glee itself, rather than any specific protocol.
|kafka.auth.username| The username to use during authentication.
|kafka.auth.password| The password to use during authentication.

### HTTP Server

|Field|Description|
|--|--|
|http.server|HTTP server-specific configurations|
|http.client|HTTP client-specific configurations|
|http.server.adapter| The Glee adapter to use for the HTTP server. Defaults to a "native" HTTP implementation.|
|websocket.server.port| The port to use when binding the HTTP server. This is useful when your server is behind a proxy and the port exposed for consumption is not the same as the port your application should be bound to. Defaults to the port specified in the selected AsyncAPI server.|
|http.client.auth| Authentication/Authorization configuration for the client|
|http.client.auth.token| HTTP Authentication header|
|http.client.query| Query object for the client to send|
|http.client.body| Body object for the client to send


### Auth Config

Expand Down
45 changes: 45 additions & 0 deletions examples/anime-http/client/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
asyncapi: 2.4.0
info:
title: AsyncAPI IMDB client
version: 1.0.0
description: This app creates a client that subscribes to the server for getting the top 10 trending/upcoming anime.
servers:
trendingAnime:
url: http://localhost:8081
protocol: http
testwebhook:
url: ws://localhost:9000
protocol: ws
x-remoteServers:
- trendingAnime
channels:
/test:
bindings:
ws:
bindingVersion: 0.1.0
publish:
operationId: index
message:
$ref: '#/components/messages/test'
trendingAnime:
bindings:
http:
type: request
method: POST
bindingVersion: 0.1.0
servers:
- trendingAnime
publish:
operationId: trendingAnimeListRecieverController
message:
$ref: "../server/asyncapi.yaml#/components/messages/trendingAnime"
subscribe:
message:
payload:
type: object
components:
messages:
test:
summary: ping client
payload:
type: object
25 changes: 25 additions & 0 deletions examples/anime-http/client/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default async function () {
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
const payload = {
body: {
name: 'test12',
rating: '5',
studio: 'teststudio',
genre: 'testgenre',
},
query: {
name: 'test34',
rating: '5',
studio: 'teststudio',
genre: 'testgenre',
}
}
return {
send: [
{
server: 'trendingAnime',
payload: payload.body,
query: payload.query
},
],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function (event) {
console.log('Trending Anime Payload:', event.payload)
}
18 changes: 18 additions & 0 deletions examples/anime-http/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "simple",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "glee start",
"dev": "glee dev"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@asyncapi/glee": "file:../../.."
}
}
10 changes: 10 additions & 0 deletions examples/anime-http/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"allowJs": true,
"target": "es6",
"esModuleInterop": true,
"moduleResolution": "node",
"module": "es2020",
"resolveJsonModule": true,
}
}
55 changes: 55 additions & 0 deletions examples/anime-http/server/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
asyncapi: 2.4.0
info:
title: AsyncAPI IMDB server
version: 1.0.0
description: This app is a dummy server that would stream the trending/upcoming anime.
servers:
trendingAnimeServer:
url: http://localhost:8081
protocol: http
channels:
trendingAnime:
bindings:
http:
type: request
method: POST
bindingVersion: 0.1.0
query:
$ref: "#/components/schemas/request"
body:
$ref: "#/components/schemas/request"
publish:
operationId: trendingAnimeController
message:
$ref: "#/components/messages/trendingAnime"
subscribe:
message:
payload:
type: object
components:
messages:
trendingAnime:
summary: Data required to populate trending anime
payload:
$ref: "#/components/schemas/request"
schemas:
request:
type: object
required:
- name
- rating
- genre
- studio
properties:
name:
type: string
description: Name of the anime.
rating:
type: string
description: Rating of the show.
genre:
type: string
description: The genre of anime.
studio:
type: string
description: The studio of anime.
12 changes: 12 additions & 0 deletions examples/anime-http/server/functions/trendingAnimeController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async function (event) {
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
const replyPayload = event.payload
const replyQuery = event.query
return {
reply: [
{
payload: replyPayload,
query: replyQuery
},
],
}
}
18 changes: 18 additions & 0 deletions examples/anime-http/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "simple",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "glee start",
"dev": "glee dev"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@asyncapi/glee": "file:../../.."
}
}
9 changes: 9 additions & 0 deletions examples/anime-http/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"allowJs": true,
"target": "es6",
"esModuleInterop": true,
"moduleResolution": "node",
"module":"es2020"
}
}
1 change: 0 additions & 1 deletion examples/crypto-websockets/client/asyncapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ components:
type: number
price:
type: number

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"emojis": "^1.0.10",
"eslint-plugin-github": "^4.3.5",
"eslint-plugin-security": "^1.4.0",
"got": "^12.5.3",
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
"kafkajs": "^2.2.3",
"mqtt": "^4.2.6",
"path-to-regexp": "^6.2.0",
Expand Down
63 changes: 63 additions & 0 deletions src/adapters/http/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Adapter from '../../lib/adapter.js'
import GleeMessage from '../../lib/message.js'
import got from 'got'
import { HttpAuthConfig, HttpAdapterConfig } from '../../lib/index.js'
import http from 'http'
class HttpClientAdapter extends Adapter {

ritik307 marked this conversation as resolved.
Show resolved Hide resolved
name(): string {
return 'HTTP client'
}
async connect(): Promise<this> {
this.emit('connect', {
name: this.name(),
adapter: this,
connection: http,
channel: this.channelNames,
})
return this
}

async send(message: GleeMessage): Promise<void> {
const headers = {}
const config: HttpAdapterConfig = await this.resolveProtocolConfig('http')
const auth: HttpAuthConfig = await this.getAuthConfig(config.client.auth)
headers['Authentication'] = auth?.token
const serverUrl = this.serverUrlExpanded
this.channelNames.forEach(async (channelName) => {
const channelInfo = this.parsedAsyncAPI.channel(channelName)
const httpChannelBinding = channelInfo.binding('http')
const channelServers = channelInfo.servers()
const isChannelServers = !channelServers.length || channelServers.includes(message.serverName)
if (
httpChannelBinding && isChannelServers
) {
const method = httpChannelBinding.method
const url = `${serverUrl}/${channelName}`
const body:any = message.payload
const query: { [key: string]: string } | { [key: string]: string[] } = message.query
got({
method,
url,
json: body,
searchParams: JSON.parse(JSON.stringify(query)),
})
.then((res) => {
const msg = this.createMessage(channelName, res.body)
this.emit('message', msg, http)
})
.catch((err) => {
this.emit('error', err)
})
}
})
}
private createMessage(channelName: string, payload: any) {
return new GleeMessage({
payload: JSON.parse(JSON.stringify(payload)),
channel: channelName,
})
}
}

export default HttpClientAdapter
Loading