From faf3be92f451c1d1c962ec3b225042deacd17893 Mon Sep 17 00:00:00 2001 From: "@s.roertgen" Date: Fri, 25 Apr 2025 10:37:15 +0200 Subject: [PATCH] Use type as array, refactor makehttprequest --- typesense30142/delete.go | 4 +-- typesense30142/query.go | 6 ++--- typesense30142/query_test.go | 3 ++- typesense30142/replace.go | 13 +++++---- typesense30142/types.go | 2 +- typesense30142/typesense.go | 51 ++++++++++++++++++++---------------- 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/typesense30142/delete.go b/typesense30142/delete.go index 287fc32..3739cc8 100644 --- a/typesense30142/delete.go +++ b/typesense30142/delete.go @@ -3,7 +3,6 @@ package typesense30142 import ( "context" "fmt" - "io" "net/http" "github.com/nbd-wtf/go-nostr" @@ -18,14 +17,13 @@ func (ts *TSBackend) DeleteEvent(ctx context.Context, event *nostr.Event) error "%s/collections/%s/documents?filter_by=d:=%s&&eventPubKey:=%s", ts.Host, ts.CollectionName, d, event.PubKey) - resp, err := ts.makehttpRequest(url, http.MethodDelete, nil) + resp, body, err := ts.makehttpRequest(url, http.MethodDelete, nil) if err != nil { return err } // Any status code other than 200 is an error if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) } diff --git a/typesense30142/query.go b/typesense30142/query.go index 6c0d7d9..4864c15 100644 --- a/typesense30142/query.go +++ b/typesense30142/query.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io" "log" "net/http" "net/url" @@ -32,7 +31,7 @@ func (ts *TSBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (chan return ch, nil } -// SearchResources searches for resources and returns both the AMB metadata and converted Nostr events +// searches for resources and returns both the AMB metadata and converted Nostr events func (ts *TSBackend) SearchResources(searchStr string) ([]nostr.Event, error) { parsedQuery := ParseSearchQuery(searchStr) @@ -59,9 +58,8 @@ func (ts *TSBackend) SearchResources(searchStr string) ([]nostr.Event, error) { // Debug information fmt.Printf("Search URL: %s\n", searchURL) - resp, err := ts.makehttpRequest(searchURL, http.MethodGet, nil) + resp, body, err := ts.makehttpRequest(searchURL, http.MethodGet, nil) - body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("error reading response body: %v", err) } diff --git a/typesense30142/query_test.go b/typesense30142/query_test.go index 87187d6..d9c2abd 100644 --- a/typesense30142/query_test.go +++ b/typesense30142/query_test.go @@ -1 +1,2 @@ -// "hello learningResourceType.prefLabel:bla" +package typesense30142 + diff --git a/typesense30142/replace.go b/typesense30142/replace.go index 597fa29..91ef71a 100644 --- a/typesense30142/replace.go +++ b/typesense30142/replace.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io" "net/http" "github.com/nbd-wtf/go-nostr" @@ -27,8 +26,7 @@ func (ts *TSBackend) eventAlreadyIndexed(doc *AMBMetadata) (*nostr.Event, error) "%s/collections/%s/documents/search?filter_by=d:=%s&&eventPubKey:=%s&q=&query_by=d,eventPubKey", ts.Host, ts.CollectionName, doc.D, doc.EventPubKey) - resp, err := ts.makehttpRequest(url, http.MethodGet, nil) - body, _ := io.ReadAll(resp.Body) + resp, body, err := ts.makehttpRequest(url, http.MethodGet, nil) if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("Search for event failed, status: %d, body: %s", resp.StatusCode, string(body)) @@ -54,14 +52,16 @@ func (ts *TSBackend) indexDocument(ctx context.Context, doc *AMBMetadata, alread } url := fmt.Sprintf("%s/collections/%s/documents", ts.Host, ts.CollectionName) - jsonData, err := json.Marshal(doc) if err != nil { return err } - resp, err := ts.makehttpRequest(url, http.MethodPost, jsonData) - body, _ := io.ReadAll(resp.Body) + resp, body, err := ts.makehttpRequest(url, http.MethodPost, jsonData) + if err != nil { + return fmt.Errorf("request failed: %v", err) + } + // Check status code and handle errors if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { return fmt.Errorf("failed to index document, status: %d, body: %s", resp.StatusCode, string(body)) } @@ -69,4 +69,3 @@ func (ts *TSBackend) indexDocument(ctx context.Context, doc *AMBMetadata, alread return nil } - diff --git a/typesense30142/types.go b/typesense30142/types.go index 5a7b45a..194673f 100644 --- a/typesense30142/types.go +++ b/typesense30142/types.go @@ -152,7 +152,7 @@ type AMBMetadata struct { ID string `json:"id"` // Document ID D string `json:"d"` - Type []string `json:"type"` + Type []string `json:"type,omitempty"` Name string `json:"name"` Description string `json:"description,omitempty"` About []*About `json:"about,omitempty"` diff --git a/typesense30142/typesense.go b/typesense30142/typesense.go index 152017c..cf3d23a 100644 --- a/typesense30142/typesense.go +++ b/typesense30142/typesense.go @@ -55,7 +55,7 @@ func (ts *TSBackend) CheckOrCreateCollection() error { func (ts *TSBackend) collectionExists() (bool, error) { url := fmt.Sprintf("%s/collections/%s", ts.Host, ts.CollectionName) - resp, err := ts.makehttpRequest(url, http.MethodGet, nil) + resp, body, err := ts.makehttpRequest(url, http.MethodGet, nil) if err != nil { return false, err } @@ -66,7 +66,6 @@ func (ts *TSBackend) collectionExists() (bool, error) { // Any status code other than 200 is an error if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(resp.Body) return false, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) } @@ -81,7 +80,7 @@ func (ts *TSBackend) createCollection(name string) error { // Base information {Name: "id", Type: "string"}, {Name: "d", Type: "string"}, - {Name: "type", Type: "string"}, + {Name: "type", Type: "string[]"}, {Name: "name", Type: "string"}, {Name: "description", Type: "string", Optional: true}, {Name: "about", Type: "object[]", Optional: true}, @@ -141,34 +140,40 @@ func (ts *TSBackend) createCollection(name string) error { return err } - resp, err := ts.makehttpRequest(url, http.MethodPost, jsonData) + resp, body, err := ts.makehttpRequest(url, http.MethodPost, jsonData) if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { - body, _ := io.ReadAll(resp.Body) return fmt.Errorf("failed to create collection, status: %d, body: %s", resp.StatusCode, string(body)) } return nil } -// Makes an http request to typesense -func (ts *TSBackend) makehttpRequest(url string, method string, reqBody []byte) (*http.Response, error) { - req, err := http.NewRequest(method, url, bytes.NewBuffer(reqBody)) - if err != nil { - return nil, err - } - - req.Header.Set("X-TYPESENSE-API-KEY", ts.ApiKey) - req.Header.Set("Content-Type", "application/json") - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - return resp, nil +func (ts *TSBackend) makehttpRequest(url string, method string, jsonData []byte) (*http.Response, []byte, error) { + // Create request + req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, nil, err + } + + req.Header.Set("X-TYPESENSE-API-KEY", ts.ApiKey) + req.Header.Set("Content-Type", "application/json") + + // Execute request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, nil, err + } + defer resp.Body.Close() + + // Read body + body, err := io.ReadAll(resp.Body) + if err != nil { + return resp, nil, err + } + + return resp, body, nil } // TODO Count events