Skip to content

Commit

Permalink
refactor create/delete dataset entry
Browse files Browse the repository at this point in the history
  • Loading branch information
consolethinks committed Aug 13, 2024
1 parent 3b19c3f commit b4bb92c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
24 changes: 14 additions & 10 deletions datasetIngestor/createDatasetEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package datasetIngestor
import (
"bytes"
"encoding/json"
"log"
"errors"
"fmt"
"net/http"
)

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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"
}
21 changes: 12 additions & 9 deletions datasetIngestor/createDatasetEntry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ 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"}`))
capturedPath = req.URL.Path
}))
// 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) {
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions datasetIngestor/deleteDatasetEntry.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b4bb92c

Please sign in to comment.