From 67dbb6421976254658c5e38045513129dd18187a Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 28 Jun 2023 23:59:50 +0200 Subject: initial public commit --- core/api.ts | 16 ++++++++++++++++ core/direct/client.ts | 11 +++++++++++ core/http/client.ts | 31 +++++++++++++++++++++++++++++++ core/http/props.ts | 10 ++++++++++ core/http/server.ts | 33 +++++++++++++++++++++++++++++++++ core/raw/api.ts | 27 +++++++++++++++++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 core/api.ts create mode 100644 core/direct/client.ts create mode 100644 core/http/client.ts create mode 100644 core/http/props.ts create mode 100644 core/http/server.ts create mode 100644 core/raw/api.ts (limited to 'core') diff --git a/core/api.ts b/core/api.ts new file mode 100644 index 0000000..0891081 --- /dev/null +++ b/core/api.ts @@ -0,0 +1,16 @@ +import { ParseResult } from "../language/types.ts"; + +/** + * @summary API interface + * + * This interface gets implemented by all API clients, so clients can be + * swapped easily. + */ +export default abstract class API { + /** @summary prepare API client */ + abstract prepare(): Promise; + + /** @summary parse sentence */ + abstract parseSentence(input: string): Promise; +}; + diff --git a/core/direct/client.ts b/core/direct/client.ts new file mode 100644 index 0000000..fda4b60 --- /dev/null +++ b/core/direct/client.ts @@ -0,0 +1,11 @@ +import API from "../api.ts"; +import YomikunRAWAPI from "../raw/api.ts"; + +/** + * @summary Yomikun direct API + * + * Alias to YomikunRAWAPI. calls API methods directly, and thus only works + * server-side. Used to test the API locally without HTTP overhead. + */ +export default class YomikunDirectAPIClient extends YomikunRAWAPI implements API { } + diff --git a/core/http/client.ts b/core/http/client.ts new file mode 100644 index 0000000..42d75f0 --- /dev/null +++ b/core/http/client.ts @@ -0,0 +1,31 @@ +import { ParseDepth, ParseResult } from "../../language/types.ts"; +import YomikunError from "../../util/error.ts"; +import API from "../api.ts"; +import { ConnectionProps, ConnectionPropsDefault } from "./props.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() { } + + async parseSentence(input: string) { + var response = await fetch(`http://${this.props.host}:${this.props.port}/parseSentence`); + console.log(response.body); + + return { + depth: ParseDepth.Term, + tokens: [], + } as ParseResult; + } +} + diff --git a/core/http/props.ts b/core/http/props.ts new file mode 100644 index 0000000..d69ae55 --- /dev/null +++ b/core/http/props.ts @@ -0,0 +1,10 @@ +export interface ConnectionProps { + host: string; + port: number; +}; + +export const ConnectionPropsDefault: ConnectionProps = { + host: "localhost", + port: 9400, +}; + diff --git a/core/http/server.ts b/core/http/server.ts new file mode 100644 index 0000000..8a6786e --- /dev/null +++ b/core/http/server.ts @@ -0,0 +1,33 @@ +import { serve } from "https://deno.land/std@0.192.0/http/server.ts"; + +import { ParseResult } from "../../language/types.ts"; +import YomikunRAWAPI from "../raw/api.ts"; +import { ConnectionProps, ConnectionPropsDefault } from "./props.ts"; + +interface Endpoint { + endpoint: string; +}; + +export default class YomikunRemoteAPIServer extends YomikunRAWAPI { + private props: ConnectionProps; + + constructor(options?: ConnectionProps) { + super(); + this.props = { ...ConnectionPropsDefault, ...options }; + } + + async parseSentence(input: string) { + return await super.parseSentence(input); + } + + async start() { + serve((req) => { + return new Response("Hello world!"); + }, { port: this.props.port }); + } + + async prepare() { + await super.prepare(); + } +} + diff --git a/core/raw/api.ts b/core/raw/api.ts new file mode 100644 index 0000000..2d29eed --- /dev/null +++ b/core/raw/api.ts @@ -0,0 +1,27 @@ +import API from "../api.ts"; +import Parser from "../../language/parser.ts"; +import YomikunError from "../../util/error.ts"; + +/** @summary internal Yomikun API client (DO NOT USE DIRECTLY) */ +export default class YomikunRAWAPI implements API { + private _parser: Parser; + + constructor() { + if (this.constructor === YomikunRAWAPI) { + throw new YomikunError("YomikunRAWAPI instantiated! please use YomikunDirectAPIClient instead"); + } + + this._parser = new Parser(); + } + + async prepare() { + await Promise.all([ + this._parser.prepare(), + ]); + } + + async parseSentence(input: string) { + return this._parser.parse(input); + } +}; + -- cgit v1.2.3