Skip to content

Commit d009a74

Browse files
committed
Add former glue code
1 parent 11b15b3 commit d009a74

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.log

README.md_

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Have a local Human Connection API server running on [port 3030](http:://localhost:3030), see [instructions](https://github.com/Human-Connection/API).
2+
3+
Make sure you have [node](https://nodejs.org/en/) installed with a version >= 9.0.0.
4+
5+
Then run:
6+
7+
```sh
8+
node --experimental-modules index.mjs
9+
10+
```

index.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import fetch from 'node-fetch';
2+
3+
const hcBackendUrl = 'http://localhost:3030'
4+
const bundestagGraphqlUrl = 'https://democracy.bundestag.io/graphql'
5+
6+
class User {
7+
login(email, password) {
8+
const formData = {
9+
email: email,
10+
password: password,
11+
strategy: 'local',
12+
}
13+
return fetch(`${hcBackendUrl}/authentication`, {
14+
method: 'post',
15+
body: JSON.stringify(formData),
16+
headers: { 'Content-Type': 'application/json' }
17+
}).then((response) => {
18+
return response.json()
19+
}).then((json) => {
20+
this.accessToken = json.accessToken;
21+
return true;
22+
});
23+
}
24+
25+
contribute(contribution){
26+
return fetch(`${hcBackendUrl}/contributions`, {
27+
method: 'post',
28+
body: JSON.stringify(contribution),
29+
headers: {
30+
'Authorization': `Bearer ${this.accessToken}`,
31+
'Content-Type': 'application/json'
32+
}
33+
}).then((response) => {
34+
return response.json();
35+
})
36+
}
37+
}
38+
39+
async function bundestagProcedures(){
40+
const query = '{"query": "{procedures(type: PREPARATION, pageSize: 1) { title abstract }}"}';
41+
return await fetch(bundestagGraphqlUrl, {
42+
method: 'post',
43+
body: query,
44+
headers: { 'Content-Type': 'application/json' }
45+
}).then((response) => {
46+
return response.json()
47+
}).then((json) => {
48+
return json.data.procedures;
49+
});
50+
}
51+
52+
53+
54+
55+
async function main() {
56+
let user = new User();
57+
await user.login('[email protected]', '1234');
58+
const procedures = await bundestagProcedures();
59+
const contribution = {
60+
title: procedures[0].title,
61+
content: procedures[0].abstract,
62+
type: "post",
63+
language: "de",
64+
categoryIds: ["5ac7768f8d655d2ee6d48fe4"] // democracy-politics
65+
}
66+
const response = await user.contribute(contribution);
67+
console.log(response);
68+
}
69+
70+
main();

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"dependencies": {
3+
"node-fetch": "^2.1.2"
4+
},
5+
"name": "human-connection-glue-code",
6+
"version": "1.0.0",
7+
"description": "Glue code between social network Human Connection and digital democracy app Democracy",
8+
"main": "index.mjs",
9+
"repository": "https://github.com/roschaefer/human-connection-glue-code",
10+
"author": "Robert Schaefer <[email protected]>",
11+
"license": "MIT"
12+
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
node-fetch@^2.1.2:
6+
version "2.1.2"
7+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"

0 commit comments

Comments
 (0)