codex_wham_usage.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. )
  9. func FetchCodexWhamUsage(
  10. ctx context.Context,
  11. client *http.Client,
  12. baseURL string,
  13. accessToken string,
  14. accountID string,
  15. ) (statusCode int, body []byte, err error) {
  16. if client == nil {
  17. return 0, nil, fmt.Errorf("nil http client")
  18. }
  19. bu := strings.TrimRight(strings.TrimSpace(baseURL), "/")
  20. if bu == "" {
  21. return 0, nil, fmt.Errorf("empty baseURL")
  22. }
  23. at := strings.TrimSpace(accessToken)
  24. aid := strings.TrimSpace(accountID)
  25. if at == "" {
  26. return 0, nil, fmt.Errorf("empty accessToken")
  27. }
  28. if aid == "" {
  29. return 0, nil, fmt.Errorf("empty accountID")
  30. }
  31. req, err := http.NewRequestWithContext(ctx, http.MethodGet, bu+"/backend-api/wham/usage", nil)
  32. if err != nil {
  33. return 0, nil, err
  34. }
  35. req.Header.Set("Authorization", "Bearer "+at)
  36. req.Header.Set("chatgpt-account-id", aid)
  37. req.Header.Set("Accept", "application/json")
  38. if req.Header.Get("originator") == "" {
  39. req.Header.Set("originator", "codex_cli_rs")
  40. }
  41. resp, err := client.Do(req)
  42. if err != nil {
  43. return 0, nil, err
  44. }
  45. defer resp.Body.Close()
  46. body, err = io.ReadAll(resp.Body)
  47. if err != nil {
  48. return resp.StatusCode, nil, err
  49. }
  50. return resp.StatusCode, body, nil
  51. }