Skip to content

Commit

Permalink
Merge pull request #6 from nyudlts/chore/tweak-instance
Browse files Browse the repository at this point in the history
add modifications to support ADOC functionality
  • Loading branch information
dmnyu authored Feb 26, 2024
2 parents 32cd930 + 8ed5a18 commit d6292e2
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 23 deletions.
10 changes: 8 additions & 2 deletions ArchivalObjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ func (a *ASClient) GetArchivalObjectIDs(repositoryID int) ([]int, error) {

func (a *ASClient) GetArchivalObject(repositoryId int, aoId int) (ArchivalObject, error) {

aoURI := fmt.Sprintf("/repositories/%d/archival_objects/%d", repositoryId, aoId)

return a.GetArchivalObjectFromURI(aoURI)
}

func (a *ASClient) GetArchivalObjectFromURI(aoURI string) (ArchivalObject, error) {

ao := ArchivalObject{}
endpoint := fmt.Sprintf("/repositories/%d/archival_objects/%d", repositoryId, aoId)

reponse, err := a.get(endpoint, true)
reponse, err := a.get(aoURI, true)
if err != nil {
return ao, err
}
Expand Down
2 changes: 1 addition & 1 deletion ArchivalObjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestArchivalObject(t *testing.T) {
if err != nil {
t.Error(err)
} else {
t.Logf("Successfully requested and serialized archival %s: %s\n", ao.URI, ao.Title)
t.Logf("Successfully requested and serialized archival object %s: %s\n", ao.URI, ao.Title)
}
})

Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## CHANGELOG

#### v0.6.0
- add `...FromURI()` functions:
- add `GetArchivalObjectFromURI()`
- add `GetDigitalObjectFromURI()`
- add `DeleteDigitalObjectFromURI()`
- add `GetDigitalObjectIDsForArchivalObjectFromURI()`
- refactor `...Object()` functions to use `...FromURI()` functions
- refactor `GetArchivalObject()` to use `GetArchivalObjectFromURI()`
- refactor `GetDigitalObject()` to use `GetDigitalObjectFromURI()`
- refactor `DeleteDigitalObject()` to use `DeleteDigitalObjectFromURI()`
- add helper type and function
- add `CreateOrUpdateResponse` type
- add `ParseCreateOrUpdateResponse()`
- force `bool` key/value pairs to always be sent in marshaled JSON for selected types
- remove `omitempty` option from `DigitalObject` type `bool` JSON tags
- remove `omitempty` option from `FileVersion` type `bool` JSON tags


21 changes: 20 additions & 1 deletion Common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

var LibraryVersion = "v0.3.14b"
var LibraryVersion = "v0.6.0"

var seed = rand.NewSource(time.Now().UnixNano())
var rGen = rand.New(seed)
Expand Down Expand Up @@ -114,3 +114,22 @@ func containsInt(list []int, id int) bool {
}
return false
}

type CreateOrUpdateResponse struct {
Status string `json:"status"`
Error string `json:"error"`
ID int `json:"id,omitempty"`
LockVersion int `json:"lock_version,omitempty"`
Stale bool `json:"stale,omitempty"`
URI string `json:"uri,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}

func ParseCreateOrUpdateResponse(body string) *CreateOrUpdateResponse {
var cour CreateOrUpdateResponse
err := json.Unmarshal([]byte(body), &cour)
if err != nil {
return nil
}
return &cour
}
47 changes: 46 additions & 1 deletion Common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package aspace

import (
"flag"
goaspacetest "github.com/nyudlts/go-aspace/goaspace_testing"
"strconv"
"testing"

goaspacetest "github.com/nyudlts/go-aspace/goaspace_testing"
)

func TestCommon(t *testing.T) {
Expand Down Expand Up @@ -33,4 +35,47 @@ func TestCommon(t *testing.T) {
t.Log("Succesfully requested valid session key.")
}
})

t.Run("Test ParseCreateOrUpdateResponse() Created", func(t *testing.T) {

responseBody := `{"status":"Created","id":61344,"lock_version":0,"stale":true,"uri":"/repositories/6/digital_objects/61344","warnings":[]}`
got := ParseCreateOrUpdateResponse(responseBody)

scenarios := [][]string{
{"Created", got.Status, "Incorrect Status"},
{"", got.Error, "Incorrect Error"},
{"61344", strconv.FormatInt(int64(got.ID), 10), "Incorrect ID"},
{"0", strconv.FormatInt(int64(got.LockVersion), 10), "Incorrect LockVersion"},
{"true", strconv.FormatBool(got.Stale), "Incorrect Stale"},
{"/repositories/6/digital_objects/61344", got.URI, "Incorrect URI"},
}

for _, scenario := range scenarios {
if scenario[1] != scenario[0] {
t.Errorf("unexpected result: %s: want: '%s', got: '%s'", scenario[2], scenario[0], scenario[1])
}
}
})

t.Run("Test ParseCreateOrUpdateResponse() Error", func(t *testing.T) {

responseBody := `{"error":"I need more coffee!"}`
got := ParseCreateOrUpdateResponse(responseBody)

scenarios := [][]string{
{"", got.Status, "Incorrect Status"},
{"I need more coffee!", got.Error, "Incorrect Error"},
{"0", strconv.FormatInt(int64(got.ID), 10), "Incorrect ID"},
{"0", strconv.FormatInt(int64(got.LockVersion), 10), "Incorrect LockVersion"},
{"false", strconv.FormatBool(got.Stale), "Incorrect Stale"},
{"", got.URI, "Incorrect URI"},
}

for _, scenario := range scenarios {
if scenario[1] != scenario[0] {
t.Errorf("unexpected result: %s: want: '%s', got: '%s'", scenario[2], scenario[0], scenario[1])
}
}
})

}
35 changes: 31 additions & 4 deletions DigitalObjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ func (a *ASClient) GetDigitalObjectIDs(repositoryId int) ([]int, error) {
}

func (a *ASClient) GetDigitalObject(repositoryId int, daoId int) (DigitalObject, error) {
doURI := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, daoId)

return a.GetDigitalObjectFromURI(doURI)
}

func (a *ASClient) GetDigitalObjectFromURI(doURI string) (DigitalObject, error) {
do := DigitalObject{}
endpoint := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, daoId)
response, err := a.get(endpoint, true)
response, err := a.get(doURI, true)
if err != nil {
return do, err
}
Expand Down Expand Up @@ -80,8 +85,13 @@ func (a *ASClient) CreateDigitalObject(repositoryId int, dao DigitalObject) (str
}

func (a *ASClient) DeleteDigitalObject(repositoryId int, doId int) (string, error) {
endpoint := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, doId)
response, err := a.delete(endpoint)
doURI := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, doId)

return a.DeleteDigitalObjectFromURI(doURI)
}

func (a *ASClient) DeleteDigitalObjectFromURI(doURI string) (string, error) {
response, err := a.delete(doURI)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -145,3 +155,20 @@ func (do DigitalObject) ContainsUseStatement(role string) bool {
}
return false
}

func (a *ASClient) GetDigitalObjectIDsForArchivalObjectFromURI(aoURI string) ([]string, error) {
doURIs := []string{}

ao, err := a.GetArchivalObjectFromURI(aoURI)
if err != nil {
return doURIs, err
}

for _, instance := range ao.Instances {
if instance.InstanceType == "digital_object" {
doURIs = append(doURIs, instance.DigitalObject["ref"])
}
}

return doURIs, nil
}
58 changes: 53 additions & 5 deletions DigitalObjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package aspace

import (
"flag"
"fmt"
goaspacetest "github.com/nyudlts/go-aspace/goaspace_testing"
"slices"
"testing"

goaspacetest "github.com/nyudlts/go-aspace/goaspace_testing"
)

func TestDigitalObject(t *testing.T) {
Expand All @@ -18,24 +19,71 @@ func TestDigitalObject(t *testing.T) {
repositoryID, digitalObjectID, err := client.GetRandomDigitalObject()
if err != nil {
t.Error(err)
t.FailNow()
}

do, err := client.GetDigitalObject(repositoryID, digitalObjectID)
if err != nil {
t.Error(err)
t.FailNow()
} else {
t.Log(fmt.Sprintf("Successfully requested and serialized digital object %s %s\n", do.URI, do.Title))
t.Logf("Successfully requested and serialized digital object %s %s\n", do.URI, do.Title)
}

})

t.Run("Test serialize a digital object using doURI", func(t *testing.T) {
doURI := "/repositories/3/digital_objects/45726"
do, err := client.GetDigitalObjectFromURI(doURI)
if err != nil {
t.Error(err)
t.FailNow()
}

want := "MSS_407_cuid29413B"
if do.DigitalObjectID != want {
t.Errorf("Expected %s but got %s", want, do.DigitalObjectID)
t.FailNow()
}
t.Logf("Successfully requested and serialized digital object via doURI %s %s\n", do.URI, do.Title)
})

t.Run("Test Unamarshal a digital object with notes", func(t *testing.T) {
t.Run("Test Unmarshal a digital object with notes", func(t *testing.T) {

do, err := client.GetDigitalObject(2, 261)
if err != nil {
t.Error(err)
} else {
t.Log(do.Notes)
t.Log(fmt.Sprintf("Successfully requested and serialized digital object %s %s\n", do.URI, do.Title))
t.Logf("Successfully requested and serialized digital object %s %s\n", do.URI, do.Title)
}
})

t.Run("Test DigitalObjectIDs from an ArchivalObject", func(t *testing.T) {

aoURI := "/repositories/3/archival_objects/912180"

got, err := client.GetDigitalObjectIDsForArchivalObjectFromURI(aoURI)
if err != nil {
t.Error(err)
t.FailNow()
}

want := []string{"/repositories/3/digital_objects/45716", "/repositories/3/digital_objects/45726", "/repositories/3/digital_objects/45717"}
if len(want) != len(got) {
t.Errorf("expected %d digital objects but got %d", len(want), len(got))
t.FailNow()
}

slices.Sort(want)
slices.Sort(got)

for i, w := range want {
if w != got[i] {
t.Errorf("expected %s but got %s", w, got[i])
t.FailNow()
}
}
t.Logf("Successfully retrieved DigitalObjectIDs for archival object: %s\n", aoURI)
})
}
14 changes: 7 additions & 7 deletions Models.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ type Deaccession struct {
Scope string `json:"scope,omitempty"`
Description string `json:"description,omitempty"`
Date Date `json:"date,omitempty"`
Reason string `json:"reason,omitempty,omitempty"`
Reason string `json:"reason,omitempty"`
Disposition string `json:"disposition,omitempty"`
Notification bool `json:"notification,omitempty"`
Extents []Extent `json:"extents,omitempty"`
Expand All @@ -198,10 +198,10 @@ type DigitalObject struct {
LockVersion *int `json:"lock_version,omitempty"`
DigitalObjectID string `json:"digital_object_id,omitempty"`
Title string `json:"title,omitempty"`
Publish bool `json:"publish,omitempty"`
Restrictions bool `json:"restrictions,omitempty"`
Suppressed bool `json:"suppressed,omitempty"`
IsSlugAuto bool `json:"is_slug_auto,omitempty"`
Publish bool `json:"publish"`
Restrictions bool `json:"restrictions"`
Suppressed bool `json:"suppressed"`
IsSlugAuto bool `json:"is_slug_auto"`
JSONModelType string `json:"jsonmodel_type,omitempty"`
ExternalIds []ExternalID `json:"external_ids,omitempty"`
Subjects []SubjectReference `json:"subjects,omitempty"`
Expand Down Expand Up @@ -270,7 +270,7 @@ type FileVersion struct {
Identifier string `json:"identifier,omitempty"`
LockVersion int `json:"lock_version,omitempty"`
FileURI string `json:"file_uri,omitempty"`
Publish bool `json:"publish,omitempty"`
Publish bool `json:"publish"`
FileFormatVersion string `json:"file_format_version,omitempty"`
FileSizeBytes uint64 `json:"file_size_bytes,omitempty"`
Checksum string `json:"checksum,omitempty"`
Expand All @@ -281,7 +281,7 @@ type FileVersion struct {
XLinkShowAttribute string `json:"xlink_show_attribute,omitempty"`
FileFormatName string `json:"file_format_name,omitempty"`
JSONModelType string `json:"jsonmodel_type,omitempty"`
IsRepresentative bool `json:"is_representative,omitempty"`
IsRepresentative bool `json:"is_representative"`
}

type Instance struct {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go-aspace v0.3.14b
# go-aspace v0.6.0
a Go library for ArchivesSpace integrations

## Use
Expand Down Expand Up @@ -57,4 +57,4 @@ to run the test suite
```shell
go test -v --config /path/to/go-aspace.yml --environment the-environment-to-use
```
The test suite will select random objects from the specified aspace instance, which may fail serialization to go structs
The test suite will select random objects from the specified aspace instance, which may fail serialization to go structs

0 comments on commit d6292e2

Please sign in to comment.