Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 6b7ae96

Browse files
generic sample generator with test
1 parent 5abbe38 commit 6b7ae96

14 files changed

+307
-64
lines changed

CHANGELOG

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# 0.5.3
22

3+
- updated package-lock.json for vulnerabilities
34
- fixed problems with latest version of docker
4-
- updated package-lock.json
55
- adding --use-version flag to use specific versions of images
6+
- adding sample.py to generate samples
7+
- reworked import to handle "too many requests"
8+
- adding checkbox to form
9+
- proxy importer now supports http and https, get and post with args
10+
- credits
611

712
# 0.5.2
813

admin/actions/deploy/iosdk/send.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ def main(args):
4242
>>> #args['io-apikey'] = "c64b38f22e8344a18d63d7c524b171cc"
4343
>>> #args['fiscal_code'] = "SCCNDR68T05L483L"
4444
>>> #print(main(args))
45-
4645
"""
4746
try:
4847
url = args['io-messages']
4948
key = args['io-apikey']
49+
code = args['fiscal_code'].split(":")[0]
5050
msg = {
5151
"content": {
5252
"subject": args['subject'],
5353
"markdown": args['markdown'],
5454
},
55-
"fiscal_code": args['fiscal_code']
55+
"fiscal_code": code
5656
}
5757
if "amount" in args and args["amount"] != "":
5858
pd = {

admin/actions/deploy/samples/env.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
def main(args):
3+
res = {}
4+
env = os.environ
5+
for k in env:
6+
if k.startswith("__OW"):
7+
res[k] = env[k]
8+
return res

admin/actions/deploy/util/import.js

+57-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
11
function main(args) {
22
if(args.url) {
3+
let url = args.url
4+
5+
// use http or https
6+
let client = require("https")
7+
if(args.url.startsWith("http:"))
8+
client = require("http")
9+
10+
// fix calls to API HOST
11+
if(url.startsWith("http://localhost:3280"))
12+
url = process.env.__OW_API_HOST + url.substring(21)
13+
14+
// build url
15+
let authURL = new URL(url);
16+
if(args.username)
17+
authURL.username = args.username;
18+
if(args.password)
19+
authURL.password = args.password
20+
21+
let body = args["jsonargs"] || "{}"
322
return new Promise(function(resolve) {
4-
let url = new URL(args.url);
5-
if(args.username)
6-
url.username = args.username;
7-
if(args.password)
8-
url.password = args.password
9-
require('https').get(url, function handleHttp(resp) {
23+
let handleHttp = (resp) => {
24+
resp.setEncoding('utf8');
1025
let data = '';
1126
resp.on('data', (chunk) => { data += chunk; });
1227
resp.on('end', () => { resolve({"body": { "data": JSON.parse(data) }})});
13-
}
14-
).on("error", (err) => {resolve({"error": err.message})})
28+
}
29+
let handleError = (err) => {resolve({"error": err.message})}
30+
if(args.useget == "true") {
31+
console.log("GET "+url)
32+
client.get(authURL, handleHttp).on("error", handleError)
33+
} else {
34+
console.log("POST "+url+" data="+body)
35+
let req = client.request(authURL, {
36+
method: "POST",
37+
headers: {
38+
'Content-Type': 'application/json',
39+
'Content-Length': body.length
40+
}
41+
}, handleHttp)
42+
req.on("error", handleError)
43+
req.write(body)
44+
req.end()
45+
}
1546
})
1647
}
1748
return { "body": { "form": [
1849
{
1950
"type": "message",
2051
"name": "note",
21-
"description": "Use url 'https://raw.githubusercontent.com/pagopa/io-sdk/master/docs/sample.json' for sample data"
52+
"description": "Import from JSON source. Replace the sample endpoint with your service."
2253
},
2354
{
2455
"name": "url",
2556
"description": "URL",
2657
"type": "string",
58+
"value": "http://localhost:3280/api/v1/web/guest/util/sample",
2759
"required": false
2860
},
2961
{
@@ -36,7 +68,21 @@ function main(args) {
3668
"name": "password",
3769
"description": "Password",
3870
"type": "password",
39-
"required": true
40-
}]
71+
"required": false
72+
},
73+
{
74+
"name": "jsonargs",
75+
"description": "Args in JSON format to POST:",
76+
"type": "string",
77+
"value": "{\"count\":1, \"fiscal_code\":\"AAAAAA00A00A000A\", \"amount\":0, \"due_date\":\"\"}",
78+
"required": false
79+
},
80+
{
81+
"name": "useget",
82+
"description": "Use GET (ignoring JSON args):",
83+
"type": "checkbox",
84+
"required": false
85+
}
86+
]
4187
} }
4288
}

admin/actions/deploy/util/sample.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import datetime
2+
import json
3+
4+
def main(args):
5+
try:
6+
print(args)
7+
count = args.get("count", 1)
8+
fiscal_code = args.get("fiscal_code", "")
9+
if fiscal_code == "":
10+
fiscal_code = "AAAAAA00A00A000A"
11+
12+
rec = {}
13+
if "amount" in args and args["amount"] != "":
14+
try: rec["amount"]=int(args["amount"])
15+
except Exception as e: print(str(e))
16+
if "due_date" in args and args["due_date"] != "":
17+
try: rec["due_date"] = datetime.datetime.fromisoformat(args["due_date"]).isoformat()
18+
except Exception as e: print(str(e))
19+
20+
res = []
21+
for i in range(0, int(count)):
22+
code = "%s:%d" % (fiscal_code, i)
23+
rec1 = rec.copy()
24+
rec1["fiscal_code"] = code
25+
rec1["subject"] = "Benvenuto %s" % code
26+
rec1["markdown"] = "# Benvenuto, %s\nTi diamo il benvenuto. Questo è un messaggio di *benvenuto* per mostrare come generare markdown in **HTML**. Ricordare che deve essere un testo lungo." % code
27+
res.append(rec1)
28+
29+
return { "body": json.dumps(res, indent=2)}
30+
except Exception as e:
31+
return { "body": { "error": str(e)}}
32+
33+
34+

admin/actions/manifest.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ packages:
3030
function: deploy/util/import.js
3131
docker: openwhisk/action-nodejs-v10
3232
web: true
33+
sample:
34+
function: deploy/util/sample.py
35+
docker: actionloop/actionloop-python-v3.7
36+
web: true
3337
iosdk:
3438
version: 1.0
3539
actions:
File renamed without changes.

admin/actions/test/import.bats

+28-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,43 @@
22
load util
33

44
@test "util/import" {
5-
65
ipost $URL/util/import
76
ckline '"name": "url"'
87
ckline '"name": "username"'
98
ckline '"name": "password"'
9+
ckline '"name": "jsonargs"'
10+
ckline '"name": "useget"'
1011
}
1112

12-
@test "util/import post" {
13-
ipost $URL/util/import url=https://raw.githubusercontent.com/pagopa/io-sdk/master/docs/sample.json
13+
@test "util/import get" {
14+
ipost $URL/util/import useget=true url=https://raw.githubusercontent.com/pagopa/io-sdk/master/docs/sample.json
1415
ckline '"fiscal_code": "AAAAAA00A00A000A"'
1516
ckline '"markdown":'
1617
ckline '"subject":'
1718
}
1819

20+
@test "util/import sample get" {
21+
ipost $URL/util/import useget=true url="http://localhost:3280/api/v1/web/guest/util/sample"
22+
ckline '"AAAAAA00A00A000A:0"'
23+
ckline '"markdown"'
24+
ckline '"subject"'
25+
ipost $URL/util/import useget=true url="http://localhost:3280/api/v1/web/guest/util/sample?count=2"
26+
ckline '"AAAAAA00A00A000A:1"'
27+
ipost $URL/util/import useget=true url="http://localhost:3280/api/v1/web/guest/util/sample?fiscal_code=BBBBB00B00B000B"
28+
ckline '"BBBBB00B00B000B:0"'
29+
}
1930

31+
@test "util/import sample post" {
32+
ipost $URL/util/import url=http://localhost:3280/api/v1/web/guest/util/sample
33+
ckline '"fiscal_code": "AAAAAA00A00A000A:0"'
34+
ckline '"markdown":'
35+
ckline '"subject":'
36+
}
37+
38+
@test "util/import sample post with args" {
39+
run http --timeout 300 --json POST $URL/util/import url=http://localhost:3280/api/v1/web/guest/util/sample jsonargs='{"count":2,"fiscal_code":"BBBBB00B00B000B","amount":1,"due_date":"2021-01-01"}'
40+
#curl -X POST -F url=http://localhost:3280/api/v1/web/guest/util/sample $URL/util/import
41+
ckline '"BBBBB00B00B000B:1"'
42+
ckline '"amount": 1'
43+
ckline '"due_date": "2021-01-01T00:00:00"'
44+
}

admin/actions/test/import.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"due_date": 44197,
66
"fiscal_code": "ISPXNB32R82Y766F",
77
"invalid_after_due_date": false,
8-
"markdown": "# Welcome, Giovanni Rossi\\\\nYour fiscal code is ISPXNB32R82Y766F\\\\nI hope you will enjoy IO.",
8+
"markdown": "# Welcome, Giovanni Rossi\nYour fiscal code is ISPXNB32R82Y766F\nI hope you will enjoy IO.",
99
"notice_number": 1,
1010
"subject": "Welcome to IO, Giovanni"
1111
},
@@ -14,7 +14,7 @@
1414
"due_date": 44197,
1515
"fiscal_code": "ISPXNB32R82Y766D",
1616
"invalid_after_due_date": false,
17-
"markdown": "# Welcome, Luca Rossi\\\\nYour fiscal code is ISPXNB32R82Y766D\\\\n.I hope you will enjoy IO.",
17+
"markdown": "# Welcome, Luca Ross\nYour fiscal code is ISPXNB32R82Y766D\n.I hope you will enjoy IO.",
1818
"notice_number": 1,
1919
"subject": "Welcome to IO, Luca"
2020
}

admin/actions/test/sample.bats

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bats
2+
load util
3+
4+
@test "util/sample post" {
5+
ipost http://localhost:3280/api/v1/web/guest/util/sample
6+
ckline '"fiscal_code": "AAAAAA00A00A000A:0"'
7+
ckline '"markdown": "# Benvenuto,'
8+
ckline '"subject": "Benvenuto'
9+
ipost http://localhost:3280/api/v1/web/guest/util/sample fiscal_code="PLMFNZ48R20I480G" due_date="2020-12-12" amount=1
10+
ckline '"amount": 1'
11+
ckline '"due_date": "2020-12-12T00:00:00"'
12+
ckline '"fiscal_code": "PLMFNZ48R20I480G:0"'
13+
ipost http://localhost:3280/api/v1/web/guest/util/sample count=2
14+
ckline '"fiscal_code": "AAAAAA00A00A000A:1"'
15+
}
16+
17+
@test "util/sample get" {
18+
get http://localhost:3280/api/v1/web/guest/util/sample
19+
ckline '"fiscal_code": "AAAAAA00A00A000A:0"'
20+
ckline '"markdown": "# Benvenuto,'
21+
ckline '"subject": "Benvenuto'
22+
}
23+
24+
@test "util/sample get count" {
25+
get "http://localhost:3280/api/v1/web/guest/util/sample?count=2"
26+
ckline '"fiscal_code": "AAAAAA00A00A000A:1"'
27+
}
28+
29+
@test "util/sample get extra" {
30+
run http --timeout=300 GET 'http://localhost:3280/api/v1/web/guest/util/sample?fiscal_code=PLMFNZ48R20I480G&due_date=2020-12-12&amount=1'
31+
ckline '"amount": 1'
32+
ckline '"due_date": "2020-12-12T00:00:00"'
33+
ckline '"fiscal_code": "PLMFNZ48R20I480G:0"'
34+
}

admin/src/About.svelte

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ let body = marked(`
66
77
IO-SDK è il kit di sviluppo per IO.
88
9-
Consente lo sviluppo di importatori per la app dei servizi pubblici italiani.
9+
Consente lo sviluppo di importatori per la app IO dei servizi pubblici italiani.
1010
11-
Il nostro team è composto da:
11+
Hanno contribuito allo sviluppo, con codice, test, documentazione, gestione e promozione le seguenti persone:
1212
13-
- Federico Feroldi, Team Leader
14-
- Mirko Calvaresi, Project Manager
15-
- Michele Sciabarrà, Developer
13+
- Andrea Saccavini
14+
- Andrea Tironi
15+
- Calogero Bonasia
16+
- Federico Feroldi
17+
- Francesco Rossi
18+
- Francesco Timperi Tiberi
19+
- Giovanni Gadaleta
20+
- Greta Quadrati
21+
- Leonardo Cigolini Gulesu
22+
- Michele Sciabarrà
23+
- Mirko Calvaresi
24+
- Pasquale Finocchiaro
25+
- Pierluigi Di Lorenzo
26+
- Simone Paolucci
1627
`);
1728
</script>
1829

admin/src/Home.svelte

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
<script>
2+
import { onMount } from 'svelte'
3+
let error = "error"
4+
5+
async function test(url) {
6+
//try {
7+
return fetch(url, {
8+
headers: { 'Accept': 'application/json'}
9+
}).then((res) => {
10+
console.log("then")
11+
console.log(res)
12+
error = res.statusText
13+
return res.json()
14+
}).catch((err) => {
15+
console.log("catch")
16+
console.log(err)
17+
error = err
18+
})
19+
//} catch(ex) { error = ex }
20+
}
21+
22+
async function start() {
23+
let r = await test("https://httpstat.us/429")
24+
console.log(r)
25+
}
26+
27+
onMount(start)
28+
29+
30+
</script>
31+
132
<div class="it-hero-wrapper">
233
<div class="container-fluid">
334
<div class="row">
@@ -6,8 +37,10 @@
637
<span class="it-category">Welcome</span>
738
<h1 class="no_toc">IO-SDK</h1>
839
<p class="d-none d-lg-block">Welcome to the development kit to easily build IO importers.</p>
40+
<br>
41+
<p>ERROR: {error}</p>
942
</div>
1043
</div>
1144
</div>
1245
</div>
13-
</div>
46+
</div>

0 commit comments

Comments
 (0)