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, format, bio string, callback func(result json.RawMessage) error) error { path := "/github/leads" params := url.Values{} if query != "" { params.Set("q", query) } if sort != "" { params.Set("sort", sort) } if exclude != "" { params.Set("exclude", exclude) } if format != "" { params.Set("format", format) } if bio != "" { params.Set("bio", bio) } fullURL := c.config.BaseURL + path if len(params) > 0 { fullURL += "?" + 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 }