From 465884631d32a66ce4402b6cbb42975494c2f524 Mon Sep 17 00:00:00 2001 From: Attila Martin Nacsa Date: Mon, 5 Aug 2024 17:23:58 +0200 Subject: [PATCH] refactor create/delete dataset entry --- datasetIngestor/createDatasetEntry.go | 24 +++++++++++++--------- datasetIngestor/createDatasetEntry_test.go | 21 +++++++++++-------- datasetIngestor/deleteDatasetEntry.go | 10 ++++++--- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/datasetIngestor/createDatasetEntry.go b/datasetIngestor/createDatasetEntry.go index 3857b6a..c03e069 100644 --- a/datasetIngestor/createDatasetEntry.go +++ b/datasetIngestor/createDatasetEntry.go @@ -3,7 +3,8 @@ package datasetIngestor import ( "bytes" "encoding/json" - "log" + "errors" + "fmt" "net/http" ) @@ -26,12 +27,13 @@ Returns: - A string representing the "pid" of the newly created dataset. If the function encounters an error, it will not return a value. Note: This function will terminate the program if it encounters an error, such as a failure to serialize the metaDataMap, a failure to send the HTTP request, a non-200 response from the server, or an unrecognized dataset type. +Note 2: This function is unused in cmd */ -func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[string]interface{}, accessToken string) (datasetId string) { +func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[string]interface{}, accessToken string) (datasetId string, err error) { // assemble json structure bm, err := json.Marshal(metaDataMap) if err != nil { - log.Fatal("Connect serialize meta data map:", metaDataMap) + return "", fmt.Errorf("couldn't marshal metadata map: %v", metaDataMap) } if val, ok := metaDataMap["type"]; ok { dstype := val.(string) @@ -47,15 +49,18 @@ func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[s } else if dstype == "base" { myurl = APIServer + "/Datasets" } else { - log.Fatal("Unknown dataset type encountered:", dstype) + return "", fmt.Errorf("unknown dataset type encountered: %v", dstype) } req, err := http.NewRequest("POST", myurl+"?access_token="+accessToken, bytes.NewBuffer(bm)) + if err != nil { + return datasetId, err + } req.Header.Set("Content-Type", "application/json") //fmt.Printf("request to message broker:%v\n", req) resp, err := client.Do(req) if err != nil { - log.Fatal(err) + return "", err } defer resp.Body.Close() if resp.StatusCode == 200 { @@ -67,15 +72,14 @@ func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[s var d PidType err := decoder.Decode(&d) if err != nil { - log.Fatal("Could not decode pid from dataset entry:", err) + return "", fmt.Errorf("could not decode pid from dataset entry: %v", err) } // fmtlog.Printf("Extracted pid:%s", d.Pid) - return d.Pid + return d.Pid, nil } else { - log.Fatalf("CreateDatasetEntry:Failed to create new dataset: status code %v\n", resp.StatusCode) + return "", fmt.Errorf("createDatasetEntry:Failed to create new dataset: status code %v", resp.StatusCode) } } else { - log.Fatal("Type of dataset not defined:") + return "", errors.New("type of dataset not defined") } - return "This should never happen" } diff --git a/datasetIngestor/createDatasetEntry_test.go b/datasetIngestor/createDatasetEntry_test.go index 799a896..bbf0fd9 100644 --- a/datasetIngestor/createDatasetEntry_test.go +++ b/datasetIngestor/createDatasetEntry_test.go @@ -8,7 +8,7 @@ import ( func TestCreateDatasetEntry(t *testing.T) { var capturedPath string - + // Create a mock server server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { rw.Write([]byte(`{"pid": "1234"}`)) @@ -16,23 +16,26 @@ func TestCreateDatasetEntry(t *testing.T) { })) // Close the server when test finishes defer server.Close() - + // Create a map for the metaData metaDataMap := map[string]interface{}{ - "type": "raw", + "type": "raw", } - + // Create a client client := server.Client() - + // Call the function - datasetId := CreateDatasetEntry(client, server.URL, metaDataMap, "testToken") - + datasetId, err := CreateDatasetEntry(client, server.URL, metaDataMap, "testToken") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + // Check the returned datasetId if datasetId != "1234" { t.Errorf("Expected datasetId to be '1234', but got '%s'", datasetId) } - + // Check the URL based on the type field expectedPath := "" switch metaDataMap["type"].(string) { @@ -43,7 +46,7 @@ func TestCreateDatasetEntry(t *testing.T) { case "base": expectedPath = "/Datasets" } - + // Check the URL if capturedPath != expectedPath { t.Errorf("Expected path to be '%s', but got '%s'", expectedPath, capturedPath) diff --git a/datasetIngestor/deleteDatasetEntry.go b/datasetIngestor/deleteDatasetEntry.go index ddfa169..16a051b 100644 --- a/datasetIngestor/deleteDatasetEntry.go +++ b/datasetIngestor/deleteDatasetEntry.go @@ -1,18 +1,22 @@ package datasetIngestor import ( - "log" "net/http" "strings" ) -func DeleteDatasetEntry(client *http.Client, APIServer string, datasetId string, accessToken string) { +// note: this function is unused in cmd after a change in datasetIngestor command +func DeleteDatasetEntry(client *http.Client, APIServer string, datasetId string, accessToken string) error { req, err := http.NewRequest("DELETE", APIServer+"/Datasets/"+strings.Replace(datasetId, "/", "%2F", 1)+"?access_token="+accessToken, nil) + if err != nil { + return err + } // fmt.Printf("Request:%v\n",req) resp, err := client.Do(req) // fmt.Printf("resp %v %v\n",resp.Body,resp.StatusCode) if err != nil { - log.Fatal(err) + return err } defer resp.Body.Close() + return nil }