Skip to content

Commit d055014

Browse files
committed
Simpler
1 parent f4136e5 commit d055014

File tree

1 file changed

+59
-47
lines changed

1 file changed

+59
-47
lines changed

lib/executor/src/executors/http.rs

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -92,50 +92,12 @@ impl HTTPSubgraphExecutor {
9292
}
9393
}
9494

95-
fn build_request_body<'exec, 'req>(
95+
fn sign_hmac(
9696
&self,
97-
execution_request: &HttpExecutionRequest<'exec, 'req>,
98-
) -> Result<Vec<u8>, SubgraphExecutorError> {
99-
let mut body = Vec::with_capacity(4096);
100-
body.put(FIRST_QUOTE_STR);
101-
write_and_escape_string(&mut body, execution_request.query);
102-
let mut first_variable = true;
103-
if let Some(variables) = &execution_request.variables {
104-
for (variable_name, variable_value) in variables {
105-
if first_variable {
106-
body.put(FIRST_VARIABLE_STR);
107-
first_variable = false;
108-
} else {
109-
body.put(COMMA);
110-
}
111-
body.put(QUOTE);
112-
body.put(variable_name.as_bytes());
113-
body.put(QUOTE);
114-
body.put(COLON);
115-
let value_str = sonic_rs::to_string(variable_value).map_err(|err| {
116-
SubgraphExecutorError::VariablesSerializationFailure(
117-
variable_name.to_string(),
118-
err.to_string(),
119-
)
120-
})?;
121-
body.put(value_str.as_bytes());
122-
}
123-
}
124-
if let Some(representations) = &execution_request.representations {
125-
if first_variable {
126-
body.put(FIRST_VARIABLE_STR);
127-
first_variable = false;
128-
} else {
129-
body.put(COMMA);
130-
}
131-
body.put("\"representations\":".as_bytes());
132-
body.extend_from_slice(representations);
133-
}
134-
// "first_variable" should be still true if there are no variables
135-
if !first_variable {
136-
body.put(CLOSE_BRACE);
137-
}
138-
97+
execution_request: &HttpExecutionRequest,
98+
body: &mut Vec<u8>,
99+
first_extension: &mut bool,
100+
) -> Result<(), SubgraphExecutorError> {
139101
let should_sign_hmac = match &self.should_sign_hmac {
140102
BooleanOrProgram::Boolean(b) => *b,
141103
BooleanOrProgram::Program(expr) => {
@@ -168,8 +130,6 @@ impl HTTPSubgraphExecutor {
168130
}
169131
};
170132

171-
let mut first_extension = true;
172-
173133
if should_sign_hmac {
174134
if self.config.hmac_signature.secret.is_empty() {
175135
return Err(SubgraphExecutorError::HMACSignatureError(
@@ -188,9 +148,9 @@ impl HTTPSubgraphExecutor {
188148
mac.update(&body_without_extensions);
189149
let result = mac.finalize();
190150
let result_bytes = result.into_bytes();
191-
if first_extension {
151+
if *first_extension {
192152
body.put(FIRST_EXTENSION_STR);
193-
first_extension = false;
153+
*first_extension = false;
194154
} else {
195155
body.put(COMMA);
196156
}
@@ -203,6 +163,58 @@ impl HTTPSubgraphExecutor {
203163
body.put(hmac_hex.as_bytes());
204164
body.put(QUOTE);
205165
}
166+
Ok(())
167+
}
168+
169+
fn build_request_body<'exec, 'req>(
170+
&self,
171+
execution_request: &HttpExecutionRequest<'exec, 'req>,
172+
) -> Result<Vec<u8>, SubgraphExecutorError> {
173+
let mut body = Vec::with_capacity(4096);
174+
body.put(FIRST_QUOTE_STR);
175+
write_and_escape_string(&mut body, execution_request.query);
176+
let mut first_variable = true;
177+
if let Some(variables) = &execution_request.variables {
178+
for (variable_name, variable_value) in variables {
179+
if first_variable {
180+
body.put(FIRST_VARIABLE_STR);
181+
first_variable = false;
182+
} else {
183+
body.put(COMMA);
184+
}
185+
body.put(QUOTE);
186+
body.put(variable_name.as_bytes());
187+
body.put(QUOTE);
188+
body.put(COLON);
189+
let value_str = sonic_rs::to_string(variable_value).map_err(|err| {
190+
SubgraphExecutorError::VariablesSerializationFailure(
191+
variable_name.to_string(),
192+
err.to_string(),
193+
)
194+
})?;
195+
body.put(value_str.as_bytes());
196+
}
197+
}
198+
if let Some(representations) = &execution_request.representations {
199+
if first_variable {
200+
body.put(FIRST_VARIABLE_STR);
201+
first_variable = false;
202+
} else {
203+
body.put(COMMA);
204+
}
205+
body.put("\"representations\":".as_bytes());
206+
body.extend_from_slice(representations);
207+
}
208+
// "first_variable" should be still true if there are no variables
209+
if !first_variable {
210+
body.put(CLOSE_BRACE);
211+
}
212+
213+
let mut first_extension = true;
214+
215+
if !self.config.hmac_signature.is_disabled() {
216+
self.sign_hmac(execution_request, &mut body, &mut first_extension)?;
217+
}
206218

207219
if let Some(extensions) = &execution_request.extensions {
208220
for (extension_name, extension_value) in extensions {

0 commit comments

Comments
 (0)