mirror of
https://github.com/edufeed-org/eventstore.git
synced 2025-12-07 23:34:32 +00:00
Use type as array, refactor makehttprequest
This commit is contained in:
parent
bf07264e17
commit
faf3be92f4
6 changed files with 40 additions and 39 deletions
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
// "hello learningResourceType.prefLabel:bla"
|
||||
package typesense30142
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue