Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
chore: provide predefined api ids in the csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Mar 27, 2024
1 parent b52f558 commit aa34eac
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 289 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ A simple script is made to do the generation based on the banker.csv file and th
- The server url for production is equal to the value in the "EndepunktProduksjon" column,
- The server url for test is equal to the value in the "EndepunktTest" column, and
- The file name of the specification is a concatenation of

- the organization number,
- the name of the organization, and
- the name of the master specification file.
- The id for each data service is equal to the "Id" column and the "TestId" column for the test-catalog. If no id is provided one is generated as a hash of the full path for the specification file.

## Development

Expand Down
244 changes: 122 additions & 122 deletions banker.csv

Large diffs are not rendered by default.

234 changes: 117 additions & 117 deletions specs/dsop_catalog.json

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions specs/test/dsop_catalog_test.json

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions src/dsop_api_spesifikasjoner/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
class API:
"""Class representing a json dataservice (API)."""

def __init__(self, url: str) -> None:
def __init__(self, url: str, predefined_id: str) -> None:
"""Inits an API."""
self.identifier = "https://dataservice-publisher.digdir.no/dataservices/{id}"
api_id = (
predefined_id
if len(predefined_id) > 0
else hashlib.sha1(str.encode(url), usedforsecurity=False).hexdigest()
)
self.identifier = (
f"https://dataservice-publisher.digdir.no/dataservices/{api_id}"
)
self.url = url
self.conformsTo: List[str] = []
self.publisher = ""
Expand All @@ -22,8 +29,12 @@ def __init__(self, production: bool) -> None:
catalog_title = "DSOP API katalog"
if not production:
catalog_title = catalog_title + " [TEST]"
id = hashlib.sha1(str.encode(catalog_title)).hexdigest() # noqa: S303,S324
self.identifier = f"https://dataservice-publisher.digdir.no/catalogs/{id}"
catalog_id = hashlib.sha1(
str.encode(catalog_title), usedforsecurity=False
).hexdigest()
self.identifier = (
f"https://dataservice-publisher.digdir.no/catalogs/{catalog_id}"
)
self.title = {"nb": catalog_title}
self.description = {"nb": "Samling av kontoopplysnings API"}
self.publisher = "https://organization-catalog.fellesdatakatalog.digdir.no/organizations/991825827" # noqa: B950
Expand Down
11 changes: 8 additions & 3 deletions src/dsop_api_spesifikasjoner/generateSpecification.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def main(template: Any, input: Any, directory: Any) -> None:
directory, specification_filename
)
_write_spec_to_file(specification_filedirectory, spec)
_add_spec_to_catalog(orgnummer, specification_filename, prod_catalog)
_add_spec_to_catalog(
orgnummer, bank[5], specification_filename, prod_catalog
)
if bank[4] and len(bank[4]) > 0: # Test
# Validate Test url:
if bank[4].endswith("/"):
Expand All @@ -79,7 +81,9 @@ def main(template: Any, input: Any, directory: Any) -> None:
directory, specification_filename
)
_write_spec_to_file(specification_filedirectory, spec)
_add_spec_to_catalog(orgnummer, specification_filename, test_catalog)
_add_spec_to_catalog(
orgnummer, bank[6], specification_filename, test_catalog
)

_write_catalog_file(prod_catalog_filename, prod_catalog)
_write_catalog_file(test_catalog_filename, test_catalog)
Expand All @@ -104,6 +108,7 @@ def _write_spec_to_file(specification_filedirectory: str, spec: dict) -> None:

def _add_spec_to_catalog(
orgnummer: str,
api_id: str,
specification_filename: str,
catalog: Catalog,
) -> None:
Expand All @@ -112,7 +117,7 @@ def _add_spec_to_catalog(
"Informasjonsforvaltning/dsop-api-spesifikasjoner/master/specs/"
f"{specification_filename}"
)
api = API(url)
api = API(url, api_id)
api.publisher = f"https://organization-catalog.fellesdatakatalog.digdir.no/organizations/{orgnummer}" # noqa: B950
api.conformsTo.append("https://bitsnorge.github.io/dsop-accounts-api")
catalog.apis.append(api)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ def test_Catalog_init(
def test_API_init() -> None:
"""Should return a catalog instance with default values."""
url_to_spec = "https://example.com/specification/oas_1"
api = API(url_to_spec)
api = API(url_to_spec, "123")
assert api
assert api.identifier == "https://dataservice-publisher.digdir.no/dataservices/{id}"
assert api.identifier == "https://dataservice-publisher.digdir.no/dataservices/123"
assert api.url == "https://example.com/specification/oas_1"


def test_add_API_to_catalog() -> None:
"""Should return a catalog instance with list of APIs."""
catalog = Catalog(production=True)
url_to_spec = "https://example.com/specification/oas_1"
api = API(url_to_spec)
api = API(url_to_spec, "123")
catalog.apis.append(api)
assert catalog.apis
assert len(catalog.apis) == 1
22 changes: 13 additions & 9 deletions tests/test_generateSpecification.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def test_main(mocker: MockerFixture, runner: CliRunner) -> None:
"""Should return exit_code 0."""
with runner.isolated_filesystem():
with open("banker.csv", "w") as f:
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest\n")
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest,Id,TestId\n")
f.write(
"837884942,SPAREBANK 1 ØSTFOLD AKERSHUS,"
"Sparebank1_837884942_Accounts-API.json,"
"https://api.sparebank1.no/dsop/Service/v2/837884942,"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942,"
","
"\n"
)
with open("template.yaml", "w") as t:
Expand Down Expand Up @@ -153,7 +154,7 @@ def test_main(mocker: MockerFixture, runner: CliRunner) -> None:
"publisher": "https://organization-catalog.fellesdatakatalog.digdir.no/organizations/991825827",
"apis": [
{
"identifier": "https://dataservice-publisher.digdir.no/dataservices/{id}",
"identifier": "https://dataservice-publisher.digdir.no/dataservices/34472b326c22e41650828da4a13ffff41d1a7cf0",
"publisher": "https://organization-catalog.fellesdatakatalog.digdir.no/organizations/837884942",
"url": "%s",
"conformsTo": [
Expand Down Expand Up @@ -195,7 +196,7 @@ def test_main(mocker: MockerFixture, runner: CliRunner) -> None:
"publisher": "https://organization-catalog.fellesdatakatalog.digdir.no/organizations/991825827",
"apis": [
{
"identifier": "https://dataservice-publisher.digdir.no/dataservices/{id}",
"identifier": "https://dataservice-publisher.digdir.no/dataservices/a9227a94f668f23b64bd676873832b70962011ee",
"publisher": "https://organization-catalog.fellesdatakatalog.digdir.no/organizations/837884942",
"url": "%s",
"conformsTo": [
Expand Down Expand Up @@ -224,12 +225,13 @@ def test_main_fails_trailing_slash_1(runner: CliRunner) -> None:
"""Should return exit_code 0."""
with runner.isolated_filesystem():
with open("banker.csv", "w") as f:
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest\n")
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest,Id,TestId\n")
f.write(
"837884942,SPAREBANK 1 ØSTFOLD AKERSHUS,"
"Sparebank1_837884942_Accounts-API.json,"
"https://api.sparebank1.no/dsop/Service/v2/837884942/,"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942,"
","
"\n"
)
with open("template.yaml", "w") as t:
Expand All @@ -251,12 +253,13 @@ def test_main_fails_trailing_slash_2(runner: CliRunner) -> None:
"""Should return exit_code 0."""
with runner.isolated_filesystem():
with open("banker.csv", "w") as f:
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest\n")
f.write("OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest,Id,TestId\n")
f.write(
"837884942,SPAREBANK 1 ØSTFOLD AKERSHUS,"
"Sparebank1_837884942_Accounts-API.json,"
"https://api.sparebank1.no/dsop/Service/v2/837884942,"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942/"
"https://api-test.sparebank1.no/dsop/Service/v2/837884942/,"
","
"\n"
)
with open("template.yaml", "w") as t:
Expand Down Expand Up @@ -297,6 +300,7 @@ def _get_bank() -> List[str]:
"SPAREBANK 1 ØSTFOLD AKERSHUS,"
"Sparebank1_837884942_Accounts-API.json,"
"https://api.sparebank1.no/Service/v2/837884942,"
"https://api-test.sparebank1.no/Service/v2/837884942"
"https://api-test.sparebank1.no/Service/v2/837884942,"
","
)
return bank_str.split(",")

0 comments on commit aa34eac

Please sign in to comment.