Skip to content

Commit

Permalink
add HTTP resolver for repo
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Jun 27, 2021
1 parent 79f1043 commit 7c31c3a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
12 changes: 3 additions & 9 deletions repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"github.com/jtacoma/uritemplates"
"github.com/whosonfirst/go-cache"
"github.com/whosonfirst/go-whosonfirst-findingaid"
"github.com/whosonfirst/go-whosonfirst-uri"
_ "log"
Expand All @@ -14,9 +13,9 @@ import (

const FINDINGAID_URI_TEMPLATE string = "https://data.whosonfirst.org/findingaid/{id}"

// RepoResolver is a struct that implements the findingaid.Resolver interface for information about Who's On First repositories by retrieving information from an HTTP endpoint that returns JSON-encoded FindingAidResponse responses. For example, a remote server running the `application/lookupd` tool.
type HTTPResolver struct {
findingaid.Resolver
cache cache.Cache
template *uritemplates.UriTemplate
}

Expand All @@ -30,6 +29,7 @@ func init() {
}
}

// NewRepoResolver returns a findingaid.Resolver instance for exposing information about Who's On First repositories by retrieving information from an HTTP endpoint that returns JSON-encoded FindingAidResponse responses.
func NewHTTPResolver(ctx context.Context, uri string) (findingaid.Resolver, error) {

u, err := url.Parse(uri)
Expand All @@ -40,12 +40,6 @@ func NewHTTPResolver(ctx context.Context, uri string) (findingaid.Resolver, erro

q := u.Query()

c, err := cache.NewCache(ctx, "gocache://")

if err != nil {
return nil, err
}

fa_template_uri := q.Get("findingaid_uri_template")

if fa_template_uri == "" {
Expand All @@ -59,13 +53,13 @@ func NewHTTPResolver(ctx context.Context, uri string) (findingaid.Resolver, erro
}

fa := &HTTPResolver{
cache: c,
template: fa_template,
}

return fa, nil
}

// ResolveURI will return 'repo.FindingAidResponse' for 'str_response' if it present in the finding aid.
func (fa *HTTPResolver) ResolveURI(ctx context.Context, str_uri string) (interface{}, error) {

id, _, err := uri.ParseURI(str_uri)
Expand Down
32 changes: 32 additions & 0 deletions repo/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package repo

import (
"context"
"github.com/whosonfirst/go-whosonfirst-findingaid"
"testing"
)

func TestHTTPResolver(t *testing.T) {

ctx := context.Background()

r, err := findingaid.NewResolver(ctx, "repo-http://")

if err != nil {
t.Fatalf("Failed to create new resolver, %v", err)
}

str_uri := "102527513"

fa_rsp, err := r.ResolveURI(ctx, str_uri)

if err != nil {
t.Fatalf("Failed to resolve '%s', %v", str_uri, err)
}

rsp := fa_rsp.(*FindingAidResponse)

if rsp.Repo != "whosonfirst-data-admin-us" {
t.Fatalf("Unexpected response: %s", rsp.Repo)
}
}
2 changes: 1 addition & 1 deletion repo/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewRepoResolver(ctx context.Context, uri string) (findingaid.Resolver, erro
return fa, nil
}

// LookupID will return 'repo.ResolverResponse' for 'id' if it present in the finding aid.
// ResolveURI will return 'repo.FindingAidResponse' for 'str_response' if it present in the finding aid.
func (fa *RepoResolver) ResolveURI(ctx context.Context, str_uri string) (interface{}, error) {

key, err := cacheKeyFromURI(str_uri)
Expand Down

0 comments on commit 7c31c3a

Please sign in to comment.