summaryrefslogtreecommitdiffstats
path: root/internal/client/skiptrace.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/client/skiptrace.go')
-rw-r--r--internal/client/skiptrace.go48
1 files changed, 43 insertions, 5 deletions
diff --git a/internal/client/skiptrace.go b/internal/client/skiptrace.go
index d4e26ea..43c2ca6 100644
--- a/internal/client/skiptrace.go
+++ b/internal/client/skiptrace.go
@@ -5,14 +5,24 @@ import (
"bytes"
"encoding/json"
"fmt"
- "git.db.org.ai/dborg/internal/models"
"io"
"net/http"
"net/url"
+ "os"
"strings"
+ "time"
+
+ "git.db.org.ai/dborg/internal/models"
)
-func parseSSEResponse(data []byte) ([]byte, error) {
+func parseSSEResponse(data []byte, debug bool) ([]byte, error) {
+ if debug {
+ fmt.Fprintln(os.Stderr, "[DEBUG] Raw SSE response:")
+ fmt.Fprintln(os.Stderr, "----------------------------------------")
+ fmt.Fprintln(os.Stderr, string(data))
+ fmt.Fprintln(os.Stderr, "----------------------------------------")
+ }
+
scanner := bufio.NewScanner(bytes.NewReader(data))
const maxScanTokenSize = 10 * 1024 * 1024
@@ -21,10 +31,15 @@ func parseSSEResponse(data []byte) ([]byte, error) {
var resultData []byte
var foundResult bool
+ var events []string
for scanner.Scan() {
line := scanner.Text()
+ if debug && strings.HasPrefix(line, "event:") {
+ events = append(events, line)
+ }
+
if line == "event: result" {
foundResult = true
continue
@@ -40,6 +55,11 @@ func parseSSEResponse(data []byte) ([]byte, error) {
return nil, fmt.Errorf("error reading SSE response: %w", err)
}
+ if debug {
+ fmt.Fprintf(os.Stderr, "[DEBUG] Events found: %v\n", events)
+ fmt.Fprintf(os.Stderr, "[DEBUG] Result event found: %v\n", foundResult)
+ }
+
if resultData == nil {
return nil, fmt.Errorf("no result event found in SSE response")
}
@@ -53,11 +73,19 @@ func parseSSEResponse(data []byte) ([]byte, error) {
}
func (c *Client) getSSE(path string, params url.Values) ([]byte, error) {
+ return c.getSSEWithTimeout(path, params, 0)
+}
+
+func (c *Client) getSSEWithTimeout(path string, params url.Values, timeout time.Duration) ([]byte, error) {
fullURL := c.config.BaseURL + path
if params != nil && len(params) > 0 {
fullURL += "?" + params.Encode()
}
+ if c.config.Debug {
+ fmt.Fprintf(os.Stderr, "[DEBUG] SSE Request: GET %s\n", fullURL)
+ }
+
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
@@ -66,12 +94,22 @@ func (c *Client) getSSE(path string, params url.Values) ([]byte, error) {
req.Header.Set("X-API-Key", c.config.APIKey)
req.Header.Set("User-Agent", c.config.UserAgent)
- resp, err := c.httpClient.Do(req)
+ httpClient := c.httpClient
+ if timeout > 0 {
+ httpClient = &http.Client{Timeout: timeout}
+ }
+
+ resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
+ if c.config.Debug {
+ fmt.Fprintf(os.Stderr, "[DEBUG] Response status: %d\n", resp.StatusCode)
+ fmt.Fprintf(os.Stderr, "[DEBUG] Content-Type: %s\n", resp.Header.Get("Content-Type"))
+ }
+
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
switch resp.StatusCode {
@@ -98,7 +136,7 @@ func (c *Client) getSSE(path string, params url.Values) ([]byte, error) {
contentType := resp.Header.Get("Content-Type")
if strings.HasPrefix(string(data), "event:") || strings.Contains(contentType, "text/event-stream") {
- return parseSSEResponse(data)
+ return parseSSEResponse(data, c.config.Debug)
}
return data, nil
@@ -135,7 +173,7 @@ func (c *Client) SearchPeople(params *models.SkiptraceParams) (*models.Skiptrace
func (c *Client) GetPersonReport(sxKey string, selection int) (*models.SkiptraceReportResponse, error) {
path := fmt.Sprintf("/prem/skiptrace/people/report/%s/%d", sxKey, selection)
- data, err := c.getSSE(path, nil)
+ data, err := c.getSSEWithTimeout(path, nil, 5*time.Minute)
if err != nil {
return nil, err
}