diff options
| author | s <[email protected]> | 2025-11-16 02:40:58 -0500 |
|---|---|---|
| committer | s <[email protected]> | 2025-11-16 02:40:58 -0500 |
| commit | 76d0cff639988ca506b1dc6e848841944c96b263 (patch) | |
| tree | b841dce1980bdb50d9f0c8f96d649a53a1778029 /internal/client | |
| parent | f4c58dfee401431c37e853643d0188cd020f66d7 (diff) | |
| download | dborg-1.0.3.tar.gz dborg-1.0.3.zip | |
docs: add comprehensive api endpoints documentation and expand cli functionalityv1.0.3
Diffstat (limited to 'internal/client')
| -rw-r--r-- | internal/client/admin.go | 14 | ||||
| -rw-r--r-- | internal/client/github.go | 123 |
2 files changed, 137 insertions, 0 deletions
diff --git a/internal/client/admin.go b/internal/client/admin.go index f4838b7..bf8c5ce 100644 --- a/internal/client/admin.go +++ b/internal/client/admin.go @@ -122,3 +122,17 @@ func (c *Client) ToggleAccount(apiKey string, enable bool) (*models.AdminRespons return &response, nil } + +func (c *Client) GetAccountStats() (*models.AccountStatsResponse, error) { + data, err := c.Get("/me", nil) + if err != nil { + return nil, err + } + + var response models.AccountStatsResponse + if err := json.Unmarshal(data, &response); err != nil { + return nil, fmt.Errorf("failed to parse account stats response: %w", err) + } + + return &response, nil +} diff --git a/internal/client/github.go b/internal/client/github.go new file mode 100644 index 0000000..f8b7097 --- /dev/null +++ b/internal/client/github.go @@ -0,0 +1,123 @@ +package client + +import ( + "bufio" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strings" +) + +func (c *Client) SearchGitHubLeads(query string, callback func(result json.RawMessage) error) error { + path := "/github/leads" + + params := url.Values{} + params.Set("q", query) + + fullURL := c.config.BaseURL + path + "?" + params.Encode() + + req, err := http.NewRequest(http.MethodGet, fullURL, nil) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("User-Agent", c.config.UserAgent) + req.Header.Set("Accept", "application/x-ndjson, application/json") + + resp, err := c.httpClient.Do(req) + if err != nil { + return fmt.Errorf("failed to execute request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body)) + } + + scanner := bufio.NewScanner(resp.Body) + for scanner.Scan() { + line := scanner.Bytes() + if len(line) == 0 { + continue + } + + if err := callback(json.RawMessage(line)); err != nil { + return err + } + } + + if err := scanner.Err(); err != nil { + if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "timeout") { + return fmt.Errorf("stream reading error: %w", err) + } + } + + return nil +} + +func (c *Client) SearchGitHubLeadsWithParams(query, sort, exclude string, callback func(result json.RawMessage) error) error { + path := "/github/leads" + + params := url.Values{} + params.Set("q", query) + if sort != "" { + params.Set("sort", sort) + } + if exclude != "" { + params.Set("exclude", exclude) + } + + fullURL := c.config.BaseURL + path + "?" + params.Encode() + + req, err := http.NewRequest(http.MethodGet, fullURL, nil) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("User-Agent", c.config.UserAgent) + req.Header.Set("Accept", "application/x-ndjson, application/json") + req.Header.Set("Connection", "keep-alive") + req.ProtoMajor = 1 + req.ProtoMinor = 1 + + http1Client := &http.Client{ + Timeout: c.config.Timeout, + Transport: &http.Transport{ + ForceAttemptHTTP2: false, + }, + } + + resp, err := http1Client.Do(req) + if err != nil { + return fmt.Errorf("failed to execute request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body)) + } + + scanner := bufio.NewScanner(resp.Body) + for scanner.Scan() { + line := scanner.Bytes() + if len(line) == 0 { + continue + } + + if err := callback(json.RawMessage(line)); err != nil { + return err + } + } + + if err := scanner.Err(); err != nil { + if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "timeout") { + return fmt.Errorf("stream reading error: %w", err) + } + } + + return nil +} |
