Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test fsprovider and hook it up to catalog. #11

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 72 additions & 72 deletions dsp/catalog_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,92 +15,51 @@
package dsp

import (
"fmt"
"io"
"net/http"

"github.com/go-dataspace/run-dsp/dsp/shared"
"github.com/go-dataspace/run-dsp/internal/auth"
"github.com/go-dataspace/run-dsp/internal/constants"
"github.com/go-dataspace/run-dsp/internal/fsprovider"
"github.com/go-dataspace/run-dsp/jsonld"
"github.com/go-dataspace/run-dsp/logging"
"github.com/go-dataspace/run-dsp/odrl"
)

// temporary data functions.
func getCatalog() CatalogAcknowledgement {
return CatalogAcknowledgement{
Dataset: Dataset{
Resource: Resource{
ID: "urn:uuid:3afeadd8-ed2d-569e-d634-8394a8836d57",
Type: "dcat:Catalog",
Keyword: []string{
"traffic",
"government",
},
Description: []Multilanguage{{
Value: "A catalog of data items",
Language: "en",
}},
},
},
Context: jsonld.NewRootContext([]jsonld.ContextEntry{{ID: constants.DSPContext}}),
Datasets: []Dataset{getDataset()},
Service: []DataService{{
Resource: Resource{
ID: "urn:uuid:4aa2dcc8-4d2d-569e-d634-8394a8834d77",
Type: "dcat:DataService",
},
EndpointDescription: "dspace:connector",
EndpointURL: "https://provider-a.com/connector",
}},
ParticipantID: "urn:example:DataProviderA",
}
var dataService = DataService{
Resource: Resource{
ID: "urn:uuid:7acb5d82-33b0-47c0-a22b-2fc470c8e3cb",
Type: "dcat:DataService",
},
EndpointURL: "https://insert-url-here.dsp/",
}

func getDataset() Dataset {
return Dataset{
func fileSetToDataset(fs shared.Fileset, service DataService) Dataset {
ds := Dataset{
Resource: Resource{
ID: "urn:uuid:3dd1add8-4d2d-569e-d634-8394a8836a88",
Type: "dcat:Dataset",
Keyword: []string{
"traffic",
},
ID: fmt.Sprintf("urn:uuid:%s", fs.ID.String()),
Type: "dcat:Dataset",
Keyword: fs.Keywords,
Description: []Multilanguage{{
Value: "Traffic data sample extract",
Value: fs.Description,
Language: "en",
}},
Title: "Traffic Data",
Title: fs.Title,
},
HasPolicy: []odrl.Offer{{
MessageOffer: odrl.MessageOffer{
PolicyClass: odrl.PolicyClass{
ID: "urn:uuid:25d74620-8f65-427c-9fc3-04ad1e19dc50",
ProviderID: "http://example.com/Provider",
Profile: []odrl.Reference{},
Permission: []odrl.Permission{{
Action: "odrl:use",
Constraint: []odrl.Constraint{{
RightOperand: "odrl:EU",
LeftOperand: "odrl:spatial",
Operator: "odrl:EQ",
}},
}},
Prohibiton: []any{},
Obligation: []odrl.Duty{},
},
Type: "odrl:Offer",
},
}},
Distribution: []Distribution{{
Type: "dcat:Distribution",
Format: "dspace:s3+push",
AccessService: []DataService{{
Resource: Resource{
ID: "urn:uuid:4aa2dcc8-4d2d-569e-d634-8394a8834d77",
Type: "dcat:DataService",
},
EndpointURL: "https://provider-a.com/connector",
}},
}},
}
dist := make([]Distribution, len(fs.Files))
for i, f := range fs.Files {
dist[i] = Distribution{
Type: "dcat:Distribution",
Format: "dspace:https+push",
Title: f.Name,
Modified: f.Modified,
AccessService: []DataService{service},
}
}
ds.Distribution = dist
return ds
}

func catalogRequestHandler(w http.ResponseWriter, req *http.Request) {
Expand All @@ -117,8 +76,34 @@ func catalogRequestHandler(w http.ResponseWriter, req *http.Request) {
return
}
logger.Debug("Got catalog request", "req", catalogReq)
p := fsprovider.New("/tmp/run-dsp-pre-alpha-storage")
ui := auth.ExtractUserInfo(req.Context())
// As there's no filter option yet, we don't need anything from the catalog request.
fileSet, err := p.GetFileSet(req.Context(), &shared.CitizenData{
FirstName: ui.FirstName,
LastName: ui.Lastname,
BirthDate: ui.BirthDate,
})
if err != nil {
returnError(w, http.StatusInternalServerError, "could not get catalog")
return
}

validateMarshalAndReturn(req.Context(), w, http.StatusOK, getCatalog())
validateMarshalAndReturn(req.Context(), w, http.StatusOK, CatalogAcknowledgement{
Dataset: Dataset{
Resource: Resource{
ID: "urn:uuid:3afeadd8-ed2d-569e-d634-8394a8836d57",
Type: "dcat:Catalog",
Keyword: []string{
"dataspace",
"run-dsp",
},
},
},
Context: jsonld.NewRootContext([]jsonld.ContextEntry{{ID: constants.DSPContext}}),
Datasets: []Dataset{fileSetToDataset(fileSet, dataService)},
Service: []DataService{dataService},
})
}

func datasetRequestHandler(w http.ResponseWriter, req *http.Request) {
Expand All @@ -140,5 +125,20 @@ func datasetRequestHandler(w http.ResponseWriter, req *http.Request) {
return
}
logger.Debug("Got dataset request", "req", datasetReq)
validateMarshalAndReturn(req.Context(), w, http.StatusOK, getDataset())
// Cheating a bit as we only have a single dataset for a user, this will be better in
// actual production code.
p := fsprovider.New("/tmp/run-dsp-pre-alpha-storage")
ui := auth.ExtractUserInfo(req.Context())
// As there's no filter option yet, we don't need anything from the catalog request.
fileSet, err := p.GetFileSet(req.Context(), &shared.CitizenData{
FirstName: ui.FirstName,
LastName: ui.Lastname,
BirthDate: ui.BirthDate,
})
if err != nil {
returnError(w, http.StatusInternalServerError, "could not get dataset")
return
}

validateMarshalAndReturn(req.Context(), w, http.StatusOK, fileSetToDataset(fileSet, dataService))
}
53 changes: 53 additions & 0 deletions dsp/shared/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 go-dataspace
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package shared contains DSP types and interfaces that both the dsp package and
// packages it imports will use.
package shared

import (
"context"
"time"

"github.com/google/uuid"
)

// File represents a file and what we can use according to the dcat distribution type.
type File struct {
Name string
Modified string
Format string
}

// Fileset is the minimal information we need to construct a dtatset.
type Fileset struct {
ID uuid.UUID
Title string
Description string
Keywords []string
Files []File
}

// CitizenData is the citizen data we will pass to the functions.
type CitizenData struct {
FirstName string
LastName string
BirthDate time.Time
}

// Cataloger exposes functions to get catalog information.
// As we only want a single dataset per citizen, we don't pass the dataset ID.
type Cataloger interface {
GetFileSet(ctx context.Context, citizenData *CitizenData) (Fileset, error)
}
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ module github.com/go-dataspace/run-dsp
go 1.22

require (
github.com/alecthomas/kong v0.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/alecthomas/kong v0.9.0
github.com/gabriel-vasile/mimetype v1.4.4
github.com/go-playground/validator/v10 v10.22.0
github.com/google/uuid v1.6.0
github.com/samber/slog-http v1.3.1
)

require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.21.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/samber/slog-http v1.3.1 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
40 changes: 30 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.21.0 h1:4fZA11ovvtkdgaeev9RGWPgc1uj3H8W+rNYyH/ySBb0=
github.com/go-playground/validator/v10 v10.21.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/slog-http v1.3.1 h1:Fho8CGX4elTKAXFKCNGloRAz2yWt1WD+vXpO9iylQ9g=
github.com/samber/slog-http v1.3.1/go.mod h1:n6h4x2ZBeTgLqMKf95EuNlU6mcJF1b/RVLxo1od5+V0=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading