11package cmd
22
33import (
4+ "bytes"
45 "fmt"
56 "io"
67 "log"
78 "net/http"
9+ "net/http/httputil"
810 "net/url"
911 "os"
1012 "strings"
@@ -14,45 +16,61 @@ import (
1416 "github.com/stainless-api/stainless-api-go"
1517 "github.com/stainless-api/stainless-api-go/option"
1618
17- "github.com/tidwall/sjson"
1819 "github.com/tidwall/gjson"
1920 "github.com/tidwall/pretty"
21+ "github.com/tidwall/sjson"
2022 "github.com/urfave/cli/v3"
2123 "golang.org/x/term"
2224)
2325
2426func getDefaultRequestOptions () []option.RequestOption {
25- return []option.RequestOption {
26- option .WithHeader ("X-Stainless-Lang" , "cli" ),
27- option .WithHeader ("X-Stainless-Runtime" , "cli" ),
28- }
27+ return append (
28+ []option.RequestOption {
29+ option .WithHeader ("X-Stainless-Lang" , "cli" ),
30+ option .WithHeader ("X-Stainless-Runtime" , "cli" ),
31+ },
32+ getClientOptions ()... ,
33+ )
2934}
3035
3136type apiCommandContext struct {
3237 client stainlessv0.Client
33- body []byte
34- query []byte
35- header []byte
38+ cmd * cli.Command
3639}
3740
3841func (c apiCommandContext ) AsMiddleware () option.Middleware {
42+ body := getStdInput ()
43+ if body == nil {
44+ body = []byte ("{}" )
45+ }
46+ var query = []byte ("{}" )
47+ var header = []byte ("{}" )
48+
49+ // Apply JSON flag mutations
50+ body , query , header , err := jsonflag .Apply (body , query , header )
51+ if err != nil {
52+ log .Fatal (err )
53+ }
54+
55+ debug := c .cmd .Bool ("debug" )
56+
3957 return func (r * http.Request , mn option.MiddlewareNext ) (* http.Response , error ) {
4058 q := r .URL .Query ()
41- for key , values := range serializeQuery (c . query ) {
59+ for key , values := range serializeQuery (query ) {
4260 for _ , value := range values {
4361 q .Set (key , value )
4462 }
4563 }
4664 r .URL .RawQuery = q .Encode ()
4765
48- for key , values := range serializeHeader (c . header ) {
66+ for key , values := range serializeHeader (header ) {
4967 for _ , value := range values {
50- r .Header .Add (key , value )
68+ r .Header .Set (key , value )
5169 }
5270 }
5371
5472 // Handle request body merging if there's a body to process
55- if r .Body != nil && len (c . body ) > 2 { // More than just "{}"
73+ if r .Body != nil || len (body ) > 2 { // More than just "{}"
5674 // Read the existing request body
5775 existingBody , err := io .ReadAll (r .Body )
5876 r .Body .Close ()
@@ -67,7 +85,7 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
6785 }
6886
6987 // Parse command body and merge top-level keys
70- commandResult := gjson .ParseBytes (c . body )
88+ commandResult := gjson .ParseBytes (body )
7189 if commandResult .IsObject () {
7290 commandResult .ForEach (func (key , value gjson.Result ) bool {
7391 // Set each top-level key from command body, overwriting existing values
@@ -82,31 +100,38 @@ func (c apiCommandContext) AsMiddleware() option.Middleware {
82100 }
83101
84102 // Set the new body
85- r .Body = io .NopCloser (strings . NewReader ( string ( mergedBody ) ))
103+ r .Body = io .NopCloser (bytes . NewBuffer ( mergedBody ))
86104 r .ContentLength = int64 (len (mergedBody ))
87105 r .Header .Set ("Content-Type" , "application/json" )
88106 }
89107
108+ // Add debug logging if the --debug flag is set
109+ if debug {
110+ logger := log .Default ()
111+
112+ if reqBytes , err := httputil .DumpRequest (r , true ); err == nil {
113+ logger .Printf ("Request Content:\n %s\n " , reqBytes )
114+ }
115+
116+ resp , err := mn (r )
117+ if err != nil {
118+ return resp , err
119+ }
120+
121+ if respBytes , err := httputil .DumpResponse (resp , true ); err == nil {
122+ logger .Printf ("Response Content:\n %s\n " , respBytes )
123+ }
124+
125+ return resp , err
126+ }
127+
90128 return mn (r )
91129 }
92130}
93131
94132func getAPICommandContext (cmd * cli.Command ) * apiCommandContext {
95133 client := stainlessv0 .NewClient (getDefaultRequestOptions ()... )
96- body := getStdInput ()
97- if body == nil {
98- body = []byte ("{}" )
99- }
100- var query = []byte ("{}" )
101- var header = []byte ("{}" )
102-
103- // Apply JSON flag mutations
104- body , query , header , err := jsonflag .Apply (body , query , header )
105- if err != nil {
106- log .Fatal (err )
107- }
108-
109- return & apiCommandContext {client , body , query , header }
134+ return & apiCommandContext {client , cmd }
110135}
111136
112137func serializeQuery (params []byte ) url.Values {
@@ -224,7 +249,7 @@ func shouldUseColors(w io.Writer) bool {
224249 return false
225250}
226251
227- func colorizeJSON (input string , w io.Writer ) string {
252+ func ColorizeJSON (input string , w io.Writer ) string {
228253 if ! shouldUseColors (w ) {
229254 return input
230255 }
0 commit comments