Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Dec 19, 2024
1 parent 72c8e69 commit 196adb1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 41 deletions.
2 changes: 1 addition & 1 deletion doi/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package doi

type Provider interface {
Init()
Publish(did, description string) (string, string, error)
Publish(did, description string, record any) (string, string, error)
}
4 changes: 2 additions & 2 deletions doi/datacite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (d *DataciteProvider) Init() {
}

// Publish provides publication of dataset with did and description
func (d *DataciteProvider) Publish(did, description string) (string, string, error) {
doi, doiLink, err := datacite.Publish(did, description)
func (d *DataciteProvider) Publish(did, description string, record any) (string, string, error) {
doi, doiLink, err := datacite.Publish(did, description, record)
return doi, doiLink, err
}
4 changes: 2 additions & 2 deletions doi/material_commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (m *MCProvider) Init() {
}

// Publish provides publication of dataset with did and description
func (m *MCProvider) Publish(did, description string) (string, string, error) {
doi, doiLink, err := materialCommons.Publish(did, description)
func (m *MCProvider) Publish(did, description string, record any) (string, string, error) {
doi, doiLink, err := materialCommons.Publish(did, description, record)
return doi, doiLink, err
}
38 changes: 2 additions & 36 deletions doi/zenodo.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,34 @@
package doi

import (
"bytes"
"encoding/json"
"fmt"
"io"

srvConfig "github.com/CHESSComputing/golib/config"
"github.com/CHESSComputing/golib/services"
"github.com/CHESSComputing/golib/zenodo"
)

// ZenodoProvider represents Zenodo provider
type ZenodoProvider struct {
HttpRequest *services.HttpRequest
Token string
}

// Init function initializes Zenodo provider
func (z *ZenodoProvider) Init() {
if srvConfig.Config == nil {
srvConfig.Init()
}
if z.HttpRequest == nil {
z.HttpRequest = services.NewHttpRequest("read", 0)
z.HttpRequest.Token = z.Token
}
}

// Publish provides publication of dataset with did and description
func (z *ZenodoProvider) Publish(did, description string) (string, string, error) {
func (z *ZenodoProvider) Publish(did, description string, record any) (string, string, error) {
var doi, doiLink string
var err error
docId, err := zenodo.CreateRecord()
if err != nil {
return doi, doiLink, err
}

// extract meta-data record for our did
query := fmt.Sprintf("{\"did\": \"%s\"}", did)
rec := services.ServiceRequest{
Client: "foxden-doi",
ServiceQuery: services.ServiceQuery{Query: query, Idx: 0, Limit: -1},
}

data, err := json.Marshal(rec)
rurl := fmt.Sprintf("%s/search", srvConfig.Config.Services.MetaDataURL)
resp, err := z.HttpRequest.Post(rurl, "application/json", bytes.NewBuffer(data))
if err != nil {
return doi, doiLink, err
}
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
return doi, doiLink, err
}
var records []map[string]any
err = json.Unmarshal(data, &records)
if err != nil {
return doi, doiLink, err
}

// add foxden record
frec := zenodo.FoxdenRecord{Did: did, MetaData: records}
frec := zenodo.FoxdenRecord{Did: did, MetaData: record}
err = zenodo.AddRecord(docId, "foxden-metadata.json", frec)
if err != nil {
return doi, doiLink, err
Expand Down

0 comments on commit 196adb1

Please sign in to comment.