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)
| 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. |
| 228 | func (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 |