aboutsummaryrefslogtreecommitdiff
path: root/core/http/client.ts
blob: a77b6160796539133c8efc5edfff939c60634c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import "../../util/array.ts";

import { ParseResult } from "../../language/types.ts";
import API from "../api.ts";
import { ConnectionProps, ConnectionPropsDefault } from "./props.ts";
import { APIRequest, APIRequestParseSentence, APIResponseParseSentence } from "./types.ts";

/**
 * @summary Yomikun HTTP API
 * 
 * Uses the Yomikun server to call API methods. Handles (de)serialization
 * automatically.
 */
export default class YomikunRemoteAPIClient implements API {
	private props: ConnectionProps;

	constructor(options?: ConnectionProps) {
		this.props = { ...ConnectionPropsDefault, ...options };
	}

	async prepare() { }

	private async request(details: APIRequest) {
		var response = await fetch(`http://${this.props.host}:${this.props.port}`, {
			method: "POST",
			headers: {
				"Accept": "application/json",
				"Content-Type": "application/json",
			},
			body: JSON.stringify(details),
		});
		return response.json();
	}

	async parseSentence(input: string) {
		var request: APIRequestParseSentence = {
			command: "parseSentence",
			options: {
				input: input,
			},
		};
		var { response } = await this.request(request) as APIResponseParseSentence;
		return response;
	}
}