2
2
use reqwest;
3
3
use serde::{Serialize, Deserialize};
4
4
use crate::datadog::*;
5
+ use reqwest::header::{HeaderMap, HeaderValue};
5
6
{% - for _ , _ , operation in operations if "x-pagination" in operation %}
6
7
{% - if loop .first %}
7
8
use async_stream::try_stream;
@@ -13,6 +14,12 @@ use futures_core::stream::Stream;
13
14
use log::warn;
14
15
{% - endif %}
15
16
{% - endfor %}
17
+ {% - for _ , _ , operation in operations if operation .requestBody is defined and not operation |form_parameter %}
18
+ {% - if loop .first %}
19
+ use std::io::Write;
20
+ use flate2::{write::{GzEncoder, ZlibEncoder}, Compression};
21
+ {% - endif %}
22
+ {% - endfor %}
16
23
17
24
{% - set structName = name .replace (" " , "" )+"API" %}
18
25
{% for path , method , operation in operations |sort (attribute ="2.operationId" , case_sensitive =true ) %}
@@ -300,18 +307,50 @@ impl {{ structName }} {
300
307
{% - endif %}
301
308
{% - endfor %}
302
309
310
+ // build headers
311
+ let mut headers = HeaderMap::new();
312
+ {% - if operation .requestBody %}
313
+ {% - set contentTypeHeaders = operation .requestBody .content .keys ()|list %}
314
+ {% - if contentTypeHeaders %}
315
+ {% - if "application/json" in contentTypeHeaders %}
316
+ headers.insert("Content-Type", HeaderValue::from_static("application/json"));
317
+ {% - else %}
318
+ headers.insert("Content-Type", HeaderValue::from_static("{{ contentTypeHeaders[0] }}"));
319
+ {% - endif %}
320
+ {% - endif %}
321
+ {% - endif %}
322
+ {% - if operation .responses %}
323
+ {% - set acceptHeaders = operation |accept_headers %}
324
+ {% - if acceptHeaders %}
325
+ {% - if "application/json" in acceptHeaders %}
326
+ headers.insert("Accept", HeaderValue::from_static("application/json"));
327
+ {% - else %}
328
+ headers.insert("Accept", HeaderValue::from_static("{{ acceptHeaders|join(",") }}"));
329
+ {% - endif %}
330
+ {% - endif %}
331
+ {% - endif %}
332
+
303
333
{% for name , parameter in operation |parameters if parameter .in == "header" %}
304
334
{% - if not parameter .required %}
305
335
if let Some(ref local) = {{name|variable_name}} {
306
- local_req_builder = local_req_builder.header ("{{name}}", & local.to_string());
307
- };
336
+ headers.insert ("{{name}}", local.to_string().parse().expect("failed to parse {{name}} header" ));
337
+ }
308
338
{% - else %}
309
- local_req_builder = local_req_builder.header ("{{name}}", & {{name|variable_name}}.to_string());
339
+ headers.insert ("{{name}}", {{name|variable_name}}.to_string().parse().expect("failed to parse {{name}} header" ));
310
340
{% - endif %}
311
341
{% - endfor %}
312
342
313
343
// build user agent
314
- local_req_builder = local_req_builder.header(reqwest::header::USER_AGENT, local_configuration.user_agent.clone());
344
+ match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
345
+ Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
346
+ Err(e) => {
347
+ log::warn!("Failed to parse user agent header: {e}, falling back to default");
348
+ headers.insert(
349
+ reqwest::header::USER_AGENT,
350
+ HeaderValue::from_static(configuration::DEFAULT_USER_AGENT.as_str()),
351
+ )
352
+ }
353
+ };
315
354
316
355
// build auth
317
356
{% - set authMethods = operation .security if "security" in operation else openapi .security %}
@@ -321,7 +360,7 @@ impl {{ structName }} {
321
360
{% - set schema = openapi .components .securitySchemes [name ] %}
322
361
{% - if schema .type == "apiKey" and schema .in != "cookie" %}
323
362
if let Some(local_key) = local_configuration.auth_keys.get("{{ name }}") {
324
- local_req_builder = local_req_builder.header ("{{schema.name}}", & local_key.key);
363
+ headers.insert ("{{schema.name}}", HeaderValue::from_str( local_key.key.as_str()).expect("failed to parse {{schema.name}} header") );
325
364
};
326
365
{% - endif %}
327
366
{% - endfor %}
@@ -333,11 +372,13 @@ impl {{ structName }} {
333
372
{% - if formParameter .required %}
334
373
let mut local_form = reqwest::multipart::Form::new();
335
374
local_form = local_form.part("{{formParameter.name}}", reqwest::multipart::Part::bytes({{formParameter.name}}).file_name("{{formParameter.name}}"));
375
+ headers.insert("Content-Type", format!("multipart/form-data; boundary={}", local_form.boundary()).parse().unwrap());
336
376
local_req_builder = local_req_builder.multipart(local_form);
337
377
{% - else %}
338
378
if let Some({{formParameter.name}}) = {{formParameter.name}} {
339
379
let mut local_form = reqwest::multipart::Form::new();
340
380
local_form = local_form.part("{{formParameter.name}}", reqwest::multipart::Part::bytes({{formParameter.name}}).file_name("{{formParameter.name}}"));
381
+ headers.insert("Content-Type", format!("multipart/form-data; boundary={}", local_form.boundary()).parse().unwrap());
341
382
local_req_builder = local_req_builder.multipart(local_form);
342
383
};
343
384
{% - endif %}
@@ -348,10 +389,55 @@ impl {{ structName }} {
348
389
let output = Vec::new();
349
390
let mut ser = serde_json::Serializer::with_formatter(output, DDFormatter);
350
391
if {{operation.get("x-codegen-request-body-name", "body")|variable_name}}.serialize(&mut ser).is_ok() {
351
- local_req_builder = local_req_builder.body(ser.into_inner());
392
+ if let Some(content_encoding) = headers.get("Content-Encoding") {
393
+ match content_encoding.to_str().unwrap_or_default() {
394
+ "gzip" => {
395
+ let mut enc = GzEncoder::new(
396
+ Vec::new(),
397
+ Compression::default(),
398
+ );
399
+ let _ = enc.write_all(ser.into_inner().as_slice());
400
+ match enc.finish() {
401
+ Ok(buf) => {
402
+ local_req_builder = local_req_builder.body(buf);
403
+ }
404
+ Err(e) => return Err(Error::Io(e)),
405
+ }
406
+ }
407
+ "deflate" => {
408
+ let mut enc = ZlibEncoder::new(
409
+ Vec::new(),
410
+ Compression::default(),
411
+ );
412
+ let _ = enc.write_all(ser.into_inner().as_slice());
413
+ match enc.finish() {
414
+ Ok(buf) => {
415
+ local_req_builder = local_req_builder.body(buf);
416
+ }
417
+ Err(e) => return Err(Error::Io(e)),
418
+ }
419
+ }
420
+ "zstd1" => {
421
+ let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
422
+ let _ = enc.write_all(ser.into_inner().as_slice());
423
+ match enc.finish() {
424
+ Ok(buf) => {
425
+ local_req_builder = local_req_builder.body(buf);
426
+ }
427
+ Err(e) => return Err(Error::Io(e)),
428
+ }
429
+ }
430
+ _ => {
431
+ local_req_builder = local_req_builder.body(ser.into_inner());
432
+ },
433
+ }
434
+ } else {
435
+ local_req_builder = local_req_builder.body(ser.into_inner());
436
+ }
352
437
}
353
438
{% - endif %}
354
439
440
+ local_req_builder = local_req_builder.headers(headers);
355
441
let local_req = local_req_builder.build()?;
356
442
let local_resp = local_client.execute(local_req).await?;
357
443
0 commit comments