1+ import { Source } from "../src" ;
2+ import { Page } from "../src/lib/Page" ;
3+ import type * as RDF from 'rdf-js' ;
4+ import { literal , namedNode , blankNode , quad } from '@rdfjs/data-model' ;
5+
6+ const fetch = require ( 'node-fetch' ) ;
7+ import * as f from "@dexagod/rdf-retrieval"
8+ import { number } from "yargs" ;
9+ const rdfParser = require ( "rdf-parse" ) . default ;
10+
11+ export class mySource extends Source {
12+
13+ private lastIndex : number = 0 ;
14+ constructor ( config : object ) {
15+ super ( config ) ;
16+ }
17+
18+ /*
19+ async getPage(id: any): Promise<Page> {
20+ // TODO: fetch API
21+
22+ // https://apidg.gent.be/opendata/adlib2eventstream/v1/dmg/objecten?generatedAtTime=2021-06-28T14:26:43.196Z
23+ let reqUrl = this.config["entrypoint"] + "?" + this.config["queryparam"] + "=" + id;
24+ let res = await this.fetchAPI(reqUrl);
25+
26+ const textStream = require('streamify-string')(res);
27+
28+ let r = await rdfParser.parse(textStream, { contentType: 'application/ld+json', baseIRI: this.config["entrypoint"] })
29+ //.on('data', (quad) => console.log(quad))
30+ .on('error', (error) => console.error(error))
31+ //.on('end', () => console.log('All done!'));
32+
33+
34+ let triples: RDF.Quad[] = await f.quadStreamToQuadArray(r)
35+
36+
37+ // const test = quad(namedNode('http://example.org/A'), namedNode('http://example.org/B'), namedNode('http://example.org/C'));
38+ // const testArray : RDF.Quad[] = [];
39+ // testArray.push(test);
40+
41+ // const p = new Page(testArray, []);
42+
43+
44+ const p = new Page(triples, []);
45+ return p;
46+ }
47+ */
48+
49+
50+ async fetchAPI ( reqUrl : String ) {
51+ return await fetch ( reqUrl )
52+ . then ( res => res . text ( ) )
53+ }
54+
55+ /*
56+
57+ // Example without index
58+ async importPages(pages: Page[]): Promise<void> {
59+ let reqUrl = this.config["entrypoint"] + "?" + this.config["queryparam"] + "=" + "2021-06-28T14:26:43.196Z";
60+ let res = await this.fetchAPI(reqUrl);
61+
62+ const textStream = require('streamify-string')(res);
63+
64+ let r = await rdfParser.parse(textStream, { contentType: 'application/ld+json', baseIRI: this.config["entrypoint"] })
65+ .on('error', (error: any) => console.error(error))
66+
67+ let triples: RDF.Quad[] = await f.quadStreamToQuadArray(r)
68+
69+ const p = new Page(triples, []);
70+
71+ let array: Page[] = [p, p];
72+
73+ super.importPages(array);
74+ }
75+ */
76+
77+ // Example with indexex
78+ async importPages ( pages : Map < string , Page > ) : Promise < void > {
79+ let triples : RDF . Quad [ ] = [ ]
80+ triples . push ( quad ( namedNode ( "observation" ) , namedNode ( 'sosa:observes' ) , literal ( ( 5 ) . toString ( ) , namedNode ( 'pedestrian' ) ) ) )
81+ triples . push ( quad ( namedNode ( "observation" ) , namedNode ( 'sosa:observes' ) , literal ( 'pedestrian' ) ) )
82+ triples . push ( quad ( namedNode ( "observation" ) , namedNode ( 'sosa:observes' ) , blankNode ( 'pedestrian' ) ) )
83+
84+ const p = new Page ( triples , [ ] ) ;
85+
86+ let map : Map < string , Page > = new Map ( ) ;
87+ map . set ( this . lastIndex . toString ( ) , p ) ;
88+ this . lastIndex ++ ;
89+
90+ super . importPages ( map ) ;
91+ }
92+
93+ }
0 commit comments