forked from rdf-ext/sparql-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpoint.js
More file actions
136 lines (116 loc) · 3.92 KB
/
Endpoint.js
File metadata and controls
136 lines (116 loc) · 3.92 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const defaultFetch = require('nodeify-fetch')
/**
* Represents a SPARQL endpoint and exposes a low-level methods, close to the underlying HTTP interface
*
* It directly returns HTTP response objects
*/
class Endpoint {
/**
* @param {Object} init
* @param {string} init.endpointUrl SPARQL Query endpoint URL
* @param {fetch} [init.fetch=nodeify-fetch] fetch implementation
* @param {HeadersInit} [init.headers] HTTP headers to send with every endpoint request
* @param {string} [init.password] password used for basic authentication
* @param {string} [init.storeUrl] Graph Store URL
* @param {string} [init.updateUrl] SPARQL Update endpoint URL
* @param {string} [init.user] user used for basic authentication
*/
constructor ({ endpointUrl, fetch, headers, password, storeUrl, updateUrl, user }) {
this.endpointUrl = endpointUrl
this.fetch = fetch || defaultFetch
this.headers = new this.fetch.Headers(headers)
this.storeUrl = storeUrl
this.updateUrl = updateUrl
if (typeof user === 'string' && typeof password === 'string') {
this.headers.set('authorization', 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64'))
}
}
/**
* Sends the query as GET request with query string
* @param {string} query SPARQL Query/Update
* @param {Object} options
* @param {HeadersInit} [options.headers] per-request HTTP headers
* @param {boolean} [options.update=false] if true, performs a SPARQL Update
* @return {Promise<Response>}
*/
async get (query, { headers, update = false } = {}) {
let url = null
if (!update) {
url = new URL(this.endpointUrl)
url.searchParams.append('query', query)
} else {
url = new URL(this.updateUrl)
url.searchParams.append('update', query)
}
return this.fetch(url.toString().replace(/\+/g, '%20'), {
method: 'GET',
headers: this.mergeHeaders(headers)
})
}
/**
* Sends the query as POST request with application/sparql-query body
* @param {string} query SPARQL Query/Update
* @param {Object} options
* @param {HeadersInit} [options.headers] per-request HTTP headers
* @param {boolean} [options.update=false] if true, performs a SPARQL Update
* @return {Promise<Response>}
*/
async postDirect (query, { headers, update = false } = {}) {
let url = null
if (!update) {
url = new URL(this.endpointUrl)
} else {
url = new URL(this.updateUrl)
}
headers = this.mergeHeaders(headers)
if (!headers.has('content-type')) {
headers.set('content-type', 'application/sparql-query; charset=utf-8')
}
return this.fetch(url, {
method: 'POST',
headers,
body: query
})
}
/**
* Sends the query as POST request with application/x-www-form-urlencoded body
* @param {string} query SPARQL Query/Update
* @param {Object} options
* @param {HeadersInit} [options.headers] per-request HTTP headers
* @param {boolean} [options.update=false] if true, performs a SPARQL Update
* @return {Promise<Response>}
*/
async postUrlencoded (query, { headers, update = false } = {}) {
let url = null
let body = null
if (!update) {
url = new URL(this.endpointUrl)
body = 'query=' + encodeURIComponent(query)
} else {
url = new URL(this.updateUrl)
body = 'update=' + encodeURIComponent(query)
}
headers = this.mergeHeaders(headers)
if (!headers.has('content-type')) {
headers.set('content-type', 'application/x-www-form-urlencoded')
}
return this.fetch(url, {
method: 'POST',
headers,
body
})
}
mergeHeaders (args = {}) {
const merged = new this.fetch.Headers()
// client headers
for (const [key, value] of this.headers) {
merged.set(key, value)
}
// argument headers
for (const [key, value] of new this.fetch.Headers(args)) {
merged.set(key, value)
}
return merged
}
}
module.exports = Endpoint