refactor use make request function

This commit is contained in:
@s.roertgen 2025-03-29 08:53:26 +01:00
parent 36952a5ffe
commit 68d3012dba

View file

@ -14,8 +14,6 @@ import (
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
) )
// TODO fix all the request stuff and put it in a dedicated function
type CollectionSchema struct { type CollectionSchema struct {
Name string `json:"name"` Name string `json:"name"`
Fields []Field `json:"fields"` Fields []Field `json:"fields"`
@ -31,10 +29,10 @@ type Field struct {
} }
type SearchResponse struct { type SearchResponse struct {
Found int `json:"found"` Found int `json:"found"`
Hits []map[string]interface{} `json:"hits"` Hits []map[string]any `json:"hits"`
Page int `json:"page"` Page int `json:"page"`
Request map[string]interface{} `json:"request"` Request map[string]any `json:"request"`
} }
// CheckOrCreateCollection checks if a collection exists and creates it if it doesn't // CheckOrCreateCollection checks if a collection exists and creates it if it doesn't
@ -90,8 +88,6 @@ func collectionExists(name string) (bool, error) {
// create a typesense collection // create a typesense collection
func createCollection(name string) error { func createCollection(name string) error {
url := fmt.Sprintf("%s/collections", typesenseHost)
schema := CollectionSchema{ schema := CollectionSchema{
Name: name, Name: name,
Fields: []Field{ Fields: []Field{
@ -151,25 +147,14 @@ func createCollection(name string) error {
EnableNestedFields: true, EnableNestedFields: true,
} }
url := fmt.Sprintf("%s/collections", typesenseHost)
jsonData, err := json.Marshal(schema) jsonData, err := json.Marshal(schema)
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData)) resp, err := makehttpRequest(url, http.MethodPost, jsonData)
if err != nil {
return err
}
req.Header.Set("X-TYPESENSE-API-KEY", apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
@ -179,41 +164,48 @@ func createCollection(name string) error {
return nil return nil
} }
// Makes an http request to typesense
func 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", 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
}
// TODO Count events // TODO Count events
func CountEvents(collectionName string, filter nostr.Filter) (int64, error) { func CountEvents(collectionName string, filter nostr.Filter) (int64, error) {
fmt.Println("filter", filter) fmt.Println("filter", filter)
// search by author // search by author
// search by d-tag // search by d-tag
return 0, nil return 0, nil
} }
// Delete a nostr event from the index // Delete a nostr event from the index
func DeleteNostrEvent(collectionName string, event *nostr.Event) error { func DeleteNostrEvent(collectionName string, event *nostr.Event) error {
fmt.Println("deleting event") fmt.Println("deleting event")
d := event.Tags.GetD() d := event.Tags.GetD()
url := fmt.Sprintf( url := fmt.Sprintf(
"%s/collections/%s/documents?filter_by=d:=%s&&eventPubKey:=%s", "%s/collections/%s/documents?filter_by=d:=%s&&eventPubKey:=%s",
typesenseHost, collectionName, d, event.PubKey) typesenseHost, collectionName, d, event.PubKey)
fmt.Println("url", url)
req, err := http.NewRequest(http.MethodDelete, url, nil) resp, err := makehttpRequest(url, http.MethodDelete, nil)
if err != nil { if err != nil {
return err return err
} }
req.Header.Set("X-TYPESENSE-API-KEY", apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// 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) body, _ := io.ReadAll(resp.Body)
@ -239,37 +231,20 @@ func eventAlreadyIndexed(collectionName string, doc *AMBMetadata) (*nostr.Event,
url := fmt.Sprintf( url := fmt.Sprintf(
"%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",
typesenseHost, collectionName, doc.D, doc.EventPubKey) typesenseHost, collectionName, doc.D, doc.EventPubKey)
fmt.Println("url", url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-TYPESENSE-API-KEY", 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()
resp, err := makehttpRequest(url, http.MethodGet, nil)
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to index document, status: %d, body: %s", resp.StatusCode, string(body)) return nil, fmt.Errorf("Search for event failed, status: %d, body: %s", resp.StatusCode, string(body))
} }
events, err := parseSearchResponse(body) events, err := parseSearchResponse(body)
if err != nil { if err != nil {
return nil, fmt.Errorf("got parsing search response: %v", err) return nil, fmt.Errorf("Error while parsing search response: %v", err)
} }
fmt.Println("response", events)
// Check if we found any events // Check if we found any events
if len(events) == 0 { if len(events) == 0 {
return nil, nil return nil, nil
} }
@ -279,9 +254,8 @@ func eventAlreadyIndexed(collectionName string, doc *AMBMetadata) (*nostr.Event,
// Index a document in Typesense // Index a document in Typesense
func indexDocument(collectionName string, doc *AMBMetadata, alreadyIndexedEvent *nostr.Event) error { func indexDocument(collectionName string, doc *AMBMetadata, alreadyIndexedEvent *nostr.Event) error {
if alreadyIndexedEvent != nil { if alreadyIndexedEvent != nil {
// delete it fmt.Println("deleting old event for new one")
fmt.Println("updating") DeleteNostrEvent(collectionName, alreadyIndexedEvent)
DeleteNostrEvent(collectionName, alreadyIndexedEvent)
} }
url := fmt.Sprintf("%s/collections/%s/documents", typesenseHost, collectionName) url := fmt.Sprintf("%s/collections/%s/documents", typesenseHost, collectionName)
@ -290,25 +264,7 @@ func indexDocument(collectionName string, doc *AMBMetadata, alreadyIndexedEvent
if err != nil { if err != nil {
return err return err
} }
method := http.MethodPost resp, err := makehttpRequest(url, http.MethodPost, jsonData)
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("X-TYPESENSE-API-KEY", apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
// Do the request
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
@ -422,20 +378,7 @@ func SearchResources(collectionName, searchStr string) ([]nostr.Event, error) {
// Debug information // Debug information
fmt.Printf("Search URL: %s\n", searchURL) fmt.Printf("Search URL: %s\n", searchURL)
// Create request resp, err := makehttpRequest(searchURL, http.MethodGet, nil)
req, err := http.NewRequest(http.MethodGet, searchURL, nil)
if err != nil {
return nil, fmt.Errorf("error creating search request: %v", err)
}
req.Header.Set("X-TYPESENSE-API-KEY", apiKey)
// Execute the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error executing search request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -460,7 +403,7 @@ func parseSearchResponse(responseBody []byte) ([]nostr.Event, error) {
for _, hit := range searchResponse.Hits { for _, hit := range searchResponse.Hits {
// Extract the document from the hit // Extract the document from the hit
docMap, ok := hit["document"].(map[string]interface{}) docMap, ok := hit["document"].(map[string]AMBMetadata)
if !ok { if !ok {
return nil, fmt.Errorf("invalid document format in search results") return nil, fmt.Errorf("invalid document format in search results")
} }