Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processing variables from external file #632

Merged

Conversation

ricmrodrigues
Copy link
Contributor

this is part of an issue I've raised, maybe missing something but I tested and it seemed to work :)

@@ -179,7 +180,10 @@ export class HttpRequestParser implements RequestParser {
const inputFilePath = groups[1];
const fileAbsolutePath = await resolveRequestBodyPath(inputFilePath);
if (fileAbsolutePath) {
combinedStream.append(fs.createReadStream(fileAbsolutePath));
const buffer = await fs.readFile(fileAbsolutePath);
const fileContent = buffer.toString('utf8');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you simply convert the buffer to UTF-8 string, if the buffer encoding is not utf8, this would make the final request body incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the best way of doing it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that VSCode interally use the library jschardet to guess textual file content encoding (https://github.com/microsoft/vscode/blob/ce1462776f7ada735ba82ffec28ea774e617a927/src/vs/workbench/services/textfile/common/encoding.ts#L234). Together with the following advice, if we add the request level setting for this feature, maybe we can also support providing an optional argument encoding, if omit, we will guess the encoding for him/her. By default the setting is turned off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we can default to utf8 and provide a way to override the default to keep it simple

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added those changes, can you check?

Comment on lines 185 to 186
const resolvedContent = await VariableProcessor.processRawRequest(fileContent);
combinedStream.append(resolvedContent);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really want to support this feature, I think we should add a switch(setting) to turn on/off this behavior that parsing the variables in the included body file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall setting? that seems a bit excessive, why not a request level setting?

Copy link
Contributor Author

@ricmrodrigues ricmrodrigues Jul 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like instead of < filename we could do <@ filename to imply it contains variables needing resolving ? What do you think @Huachao

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this with an optional encoding, seems like a nice option, brings in the default with an option to override and only processes variables in files if we signal it that way with the @

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice suggestion

@@ -179,7 +180,10 @@ export class HttpRequestParser implements RequestParser {
const inputFilePath = groups[1];
const fileAbsolutePath = await resolveRequestBodyPath(inputFilePath);
if (fileAbsolutePath) {
combinedStream.append(fs.createReadStream(fileAbsolutePath));
const buffer = await fs.readFile(fileAbsolutePath);
const fileContent = buffer.toString('utf8');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that VSCode interally use the library jschardet to guess textual file content encoding (https://github.com/microsoft/vscode/blob/ce1462776f7ada735ba82ffec28ea774e617a927/src/vs/workbench/services/textfile/common/encoding.ts#L234). Together with the following advice, if we add the request level setting for this feature, maybe we can also support providing an optional argument encoding, if omit, we will guess the encoding for him/her. By default the setting is turned off.

…flagged when referencing the file via an @ sign '<@' and optionally the encoding name (utf8 by default) '<@Latin1'
const resolvedContent = await VariableProcessor.processRawRequest(fileContent);
combinedStream.append(resolvedContent);
}
else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the tslint error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -21,7 +22,8 @@ enum ParseState {
export class HttpRequestParser implements RequestParser {
private readonly defaultMethod = 'GET';
private readonly queryStringLinePrefix = /^\s*[&\?]/;
private readonly inputFileSyntax = /^<\s+(.+?)\s*$/;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentLInkProvider.ts also uses the same regex to identify the file link, you also need to update the regex there since the Click link feature is broken when appendig @ after <

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good point, noticed the underscore stopped working after my change!

@@ -7,7 +7,7 @@ import { getWorkspaceRootPath } from '../utils/workspaceUtility';

export class RequestBodyDocumentLinkProvider implements DocumentLinkProvider {

private _linkPattern = /^(\<\s+)(.+)(\s*)$/g;
private _linkPattern = /^(\<(?:@?(?:\w+)?)?\s+)(.+)(\s*)$/g;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems in your regex, the encoding is supported with the < syntax, in my option, it's only supported with the <@ syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted

@Huachao Huachao merged commit a204832 into Huachao:master Jul 8, 2020
@Huachao
Copy link
Owner

Huachao commented Jul 8, 2020

@ricmrodrigues Merged, thanks

@ricmrodrigues ricmrodrigues deleted the adding-external-file-body-variables branch July 8, 2020 10:02
@jackbravo
Copy link
Contributor

What was missing was documenting how this feature works in the README file.

@ricmrodrigues
Copy link
Contributor Author

What was missing was documenting how this feature works in the README file.

#637

@Huachao
Copy link
Owner

Huachao commented Aug 30, 2020

@ricmrodrigues you can try the latest version 0.24.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants