MCPcopy Index your code
hub / github.com/coder/coder / RequestWithoutSessionToken

Method RequestWithoutSessionToken

codersdk/client.go:228–364  ·  view source on GitHub ↗

RequestWithoutSessionToken performs a HTTP request. It is similar to Request, but does not set the session token in the request header, nor does it make a call to the SessionTokenProvider. This allows session token providers to call this method without causing reentrancy issues.

(ctx context.Context, method, path string, body interface{}, opts ...RequestOption)

Source from the content-addressed store, hash-verified

226// the session token in the request header, nor does it make a call to the SessionTokenProvider.
227// This allows session token providers to call this method without causing reentrancy issues.
228func (c *Client) RequestWithoutSessionToken(ctx context.Context, method, path string, body interface{}, opts ...RequestOption) (*http.Response, error) {
229 if ctx == nil {
230 return nil, xerrors.Errorf("context should not be nil")
231 }
232 ctx, span := tracing.StartSpanWithName(ctx, tracing.FuncNameSkip(1))
233 defer span.End()
234
235 serverURL, err := c.URL.Parse(path)
236 if err != nil {
237 return nil, xerrors.Errorf("parse url: %w", err)
238 }
239
240 var r io.Reader
241 if body != nil {
242 switch data := body.(type) {
243 case io.Reader:
244 r = data
245 case []byte:
246 r = bytes.NewReader(data)
247 default:
248 // Assume JSON in all other cases.
249 buf := bytes.NewBuffer(nil)
250 enc := json.NewEncoder(buf)
251 enc.SetEscapeHTML(false)
252 err = enc.Encode(body)
253 if err != nil {
254 return nil, xerrors.Errorf("encode body: %w", err)
255 }
256 r = buf
257 }
258 }
259
260 // Copy the request body so we can log it.
261 var reqLogFields []slog.Field
262 c.mu.RLock()
263 logBodies := c.logBodies
264 c.mu.RUnlock()
265 if r != nil && logBodies {
266 reqBody, err := io.ReadAll(r)
267 if err != nil {
268 return nil, xerrors.Errorf("read request body: %w", err)
269 }
270 r = bytes.NewReader(reqBody)
271 reqLogFields = append(reqLogFields, slog.F("body", string(reqBody)))
272 }
273
274 req, err := http.NewRequestWithContext(ctx, method, serverURL.String(), r)
275 if err != nil {
276 return nil, xerrors.Errorf("create request: %w", err)
277 }
278
279 if r != nil {
280 req.Header.Set("Content-Type", "application/json")
281 }
282 for _, opt := range opts {
283 opt(req)
284 }
285

Callers 5

RequestMethod · 0.95
postAWSInstanceIdentityFunction · 0.80
exchangeMethod · 0.80
exchangeMethod · 0.80
exchangeMethod · 0.80

Calls 15

LoggerMethod · 0.95
StartSpanWithNameFunction · 0.92
FuncNameSkipFunction · 0.92
RunWithoutSpanFunction · 0.92
prefixLinesFunction · 0.85
parseMimeTypeFunction · 0.85
EncodeMethod · 0.80
ParseMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65
DumpRequestMethod · 0.65
WriteMethod · 0.65

Tested by 1

postAWSInstanceIdentityFunction · 0.64