Skip to content

Commit 48f892a

Browse files
committed
Add basic classes
1 parent 62ab686 commit 48f892a

6 files changed

Lines changed: 102 additions & 55 deletions

File tree

src/connection.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
// https://developer.amazon.com/docs/alexa-voice-service/manage-http2-connection.html
2-
import * as https from "https";
3-
import * as spdy from "spdy";
2+
import * as http2 from "http2";
3+
import {API_VERSION} from "./constants/general";
44

55
export default class Connection {
6-
constructor(private readonly agent: spdy.Agent, private readonly endpoint: string) {}
6+
constructor(private readonly client: http2.ClientHttp2Session) {}
77

88
public connect(accessToken: string): void {
9-
const req = https.get(
10-
{
11-
host: `${this.endpoint}/directives`,
12-
agent: this.agent,
13-
headers: {
14-
authorization: `Bearer ${accessToken}`,
15-
},
16-
},
17-
(response) => {
18-
// TODO
19-
},
20-
);
21-
22-
req.on("push", (stream) => {
23-
stream.on("error", (err) => {
24-
// Handle error
25-
});
26-
// Read data from stream
9+
const req = this.client.request({
10+
":path": `/${API_VERSION}/directives`,
11+
authorization: `Bearer ${accessToken}`,
2712
});
13+
14+
req.end();
2815
}
2916
}

src/constants/general.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const API_VERSION = "v20160207";
2+
export const HTTP2_BOUNDARY = "this-is-a-boundary";

src/http2-utility.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {HTTP2_BOUNDARY} from "./constants/general";
2+
3+
export default class Http2Utility {
4+
public createMetadata<T>(body: T): string {
5+
const output = `--${HTTP2_BOUNDARY}
6+
Content-Disposition: form-data; name="metadata"
7+
Content-Type: application/json; charset=UTF-8
8+
9+
${JSON.stringify(body)}
10+
11+
--HTTP2_BOUNDARY--
12+
`;
13+
14+
return output;
15+
}
16+
}

src/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import * as https from "https";
2-
import * as spdy from "spdy";
1+
import * as http2 from "http2";
2+
3+
import Connection from "./connection";
4+
import {BASE_URLS} from "./constants/base-urls";
5+
import {API_VERSION} from "./constants/general";
36
import SpeechRecognizer from "./speech-recognizer";
7+
import System from "./system";
8+
9+
const client = http2.connect(`${BASE_URLS.europe}`);
10+
client.on("error", (err) => console.error(err));
11+
client.on("socketError", (err) => console.error(err));
12+
13+
const context: AVS.Context = [];
414

5-
const agent = spdy.createAgent({
6-
host: "www.google.com",
7-
port: 443,
15+
const speechSynthesizer = new SpeechRecognizer(client);
16+
const connection = new Connection(client);
17+
const system = new System(client);
818

9-
// Optional SPDY options
10-
spdy: {
11-
plain: false,
12-
ssl: true,
13-
// **optional** send X_FORWARDED_FOR
14-
"x-forwarded-for": "127.0.0.1",
15-
},
16-
});
19+
const accessToken = "";
1720

18-
const s = new SpeechRecognizer(agent);
21+
connection.connect(accessToken);
22+
system.synchronizeState(accessToken, context);

src/speech-recognizer.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
// https://developer.amazon.com/docs/alexa-voice-service/speechrecognizer.html
2-
import * as https from "https";
3-
import * as spdy from "spdy";
2+
import * as http2 from "http2";
43

54
export default class SpeechRecognizer {
6-
constructor(private agent: spdy.Agent) {}
5+
constructor(private readonly client: http2.ClientHttp2Session) {}
76

87
public fire(): void {
9-
const req = https.get(
10-
{
11-
host: "https://avs-alexa-eu.amazon.com",
12-
agent: this.agent,
13-
},
14-
(response) => {
15-
// TODO
16-
},
17-
);
18-
19-
req.on("push", (stream) => {
20-
stream.on("error", (err) => {
21-
// Handle error
22-
});
23-
// Read data from stream
24-
});
8+
// Todo
259
}
2610
}

src/system.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
11
// https://developer.amazon.com/docs/alexa-voice-service/system.html
2+
import * as http2 from "http2";
23

3-
export default class System {}
4+
import {API_VERSION, HTTP2_BOUNDARY} from "./constants/general";
5+
import Http2Utility from "./http2-utility";
6+
7+
export default class System {
8+
private readonly http2Utility: Http2Utility;
9+
10+
constructor(private readonly client: http2.ClientHttp2Session) {
11+
this.http2Utility = new Http2Utility();
12+
}
13+
14+
public synchronizeState(accessToken: string, context: AVS.Context): void {
15+
const req = this.client.request({
16+
":method": "POST",
17+
":path": `/${API_VERSION}/events`,
18+
authorization: `Bearer ${accessToken}`,
19+
"content-type": `multipart/form-data; boundary=${HTTP2_BOUNDARY}`,
20+
});
21+
22+
console.log("okie");
23+
24+
// req.on("response", (headers, flags) => {
25+
// for (const name in headers) {
26+
// console.log(`${name}: ${headers[name]}`);
27+
// }
28+
// });
29+
30+
const f = this.http2Utility.createMetadata({
31+
context: context,
32+
event: {
33+
header: {
34+
namespace: "System",
35+
name: "SynchronizeState",
36+
messageId: "test",
37+
},
38+
payload: {},
39+
},
40+
});
41+
42+
req.write(f, () => {
43+
console.log("dun knoe");
44+
console.log();
45+
});
46+
47+
req.setEncoding("utf8");
48+
let data = "";
49+
req.on("data", (chunk) => {
50+
data += chunk;
51+
});
52+
req.on("end", () => {
53+
console.log(`\n${data}`);
54+
// client.close();
55+
});
56+
req.end();
57+
}
58+
}

0 commit comments

Comments
 (0)