Use type as array, refactor makehttprequest

This commit is contained in:
@s.roertgen 2025-04-25 10:37:15 +02:00
parent bf07264e17
commit faf3be92f4
6 changed files with 40 additions and 39 deletions

View file

@ -3,7 +3,6 @@ package typesense30142
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"net/http" "net/http"
"github.com/nbd-wtf/go-nostr" "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", "%s/collections/%s/documents?filter_by=d:=%s&&eventPubKey:=%s",
ts.Host, ts.CollectionName, d, event.PubKey) 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 { if err != nil {
return err return err
} }
// Any status code other than 200 is an error // Any status code other than 200 is an error
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
} }

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -32,7 +31,7 @@ func (ts *TSBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (chan
return ch, nil 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) { func (ts *TSBackend) SearchResources(searchStr string) ([]nostr.Event, error) {
parsedQuery := ParseSearchQuery(searchStr) parsedQuery := ParseSearchQuery(searchStr)
@ -59,9 +58,8 @@ func (ts *TSBackend) SearchResources(searchStr string) ([]nostr.Event, error) {
// Debug information // Debug information
fmt.Printf("Search URL: %s\n", searchURL) 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 { if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err) return nil, fmt.Errorf("error reading response body: %v", err)
} }

View file

@ -1 +1,2 @@
// "hello learningResourceType.prefLabel:bla" package typesense30142

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"github.com/nbd-wtf/go-nostr" "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", "%s/collections/%s/documents/search?filter_by=d:=%s&&eventPubKey:=%s&q=&query_by=d,eventPubKey",
ts.Host, ts.CollectionName, doc.D, doc.EventPubKey) ts.Host, ts.CollectionName, doc.D, doc.EventPubKey)
resp, err := ts.makehttpRequest(url, http.MethodGet, nil) resp, body, err := ts.makehttpRequest(url, http.MethodGet, nil)
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Search for event failed, status: %d, body: %s", resp.StatusCode, string(body)) 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) url := fmt.Sprintf("%s/collections/%s/documents", ts.Host, ts.CollectionName)
jsonData, err := json.Marshal(doc) jsonData, err := json.Marshal(doc)
if err != nil { if err != nil {
return err return err
} }
resp, err := ts.makehttpRequest(url, http.MethodPost, jsonData) resp, body, err := ts.makehttpRequest(url, http.MethodPost, jsonData)
body, _ := io.ReadAll(resp.Body) 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 { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("failed to index document, status: %d, body: %s", resp.StatusCode, string(body)) 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 return nil
} }

View file

@ -152,7 +152,7 @@ type AMBMetadata struct {
ID string `json:"id"` ID string `json:"id"`
// Document ID // Document ID
D string `json:"d"` D string `json:"d"`
Type []string `json:"type"` Type []string `json:"type,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
About []*About `json:"about,omitempty"` About []*About `json:"about,omitempty"`

View file

@ -55,7 +55,7 @@ func (ts *TSBackend) CheckOrCreateCollection() error {
func (ts *TSBackend) collectionExists() (bool, error) { func (ts *TSBackend) collectionExists() (bool, error) {
url := fmt.Sprintf("%s/collections/%s", ts.Host, ts.CollectionName) 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 { if err != nil {
return false, err return false, err
} }
@ -66,7 +66,6 @@ func (ts *TSBackend) collectionExists() (bool, error) {
// Any status code other than 200 is an error // Any status code other than 200 is an error
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return false, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(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 // Base information
{Name: "id", Type: "string"}, {Name: "id", Type: "string"},
{Name: "d", Type: "string"}, {Name: "d", Type: "string"},
{Name: "type", Type: "string"}, {Name: "type", Type: "string[]"},
{Name: "name", Type: "string"}, {Name: "name", Type: "string"},
{Name: "description", Type: "string", Optional: true}, {Name: "description", Type: "string", Optional: true},
{Name: "about", Type: "object[]", Optional: true}, {Name: "about", Type: "object[]", Optional: true},
@ -141,34 +140,40 @@ func (ts *TSBackend) createCollection(name string) error {
return err 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 { 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 fmt.Errorf("failed to create collection, status: %d, body: %s", resp.StatusCode, string(body))
} }
return nil return nil
} }
// Makes an http request to typesense func (ts *TSBackend) makehttpRequest(url string, method string, jsonData []byte) (*http.Response, []byte, error) {
func (ts *TSBackend) makehttpRequest(url string, method string, reqBody []byte) (*http.Response, error) { // Create request
req, err := http.NewRequest(method, url, bytes.NewBuffer(reqBody)) req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
req.Header.Set("X-TYPESENSE-API-KEY", ts.ApiKey) req.Header.Set("X-TYPESENSE-API-KEY", ts.ApiKey)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
// Execute request
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
return resp, nil // Read body
body, err := io.ReadAll(resp.Body)
if err != nil {
return resp, nil, err
}
return resp, body, nil
} }
// TODO Count events // TODO Count events