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

feat: dart support #376

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ Array [
"key": "csharp",
"title": "C#",
},
Object {
"clients": Array [
Object {
"description": "Dart HTTP client request using the http package",
"key": "http",
"link": "https://pub.dev/packages/http",
"title": "HTTP",
},
],
"default": "http",
"extname": ".dart",
"key": "dart",
"title": "Dart",
},
Object {
"clients": Array [
Object {
Expand Down
40 changes: 40 additions & 0 deletions src/targets/dart/http/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import full from '../../../fixtures/requests/full.json';
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures';
import { Request } from '../../../httpsnippet';

runCustomFixtures({
targetId: 'dart',
clientId: 'http',
tests: [
{
it: 'should support false boilerplate option',
input: full as Request,
options: {
showBoilerplate: false,
},
expected: 'boilerplate-option.dart',
},
{
it: 'should support printBody option',
input: full as Request,
options: {
printBody: false,
},
expected: 'print-body-option.dart',
},
{
it: 'should support timeout option',
input: full as Request,
options: {
timeout: 30,
},
expected: 'timeout-option.dart',
},
{
it: 'should generate full request',
input: full as Request,
options: {},
expected: 'full.dart',
},
],
});
101 changes: 101 additions & 0 deletions src/targets/dart/http/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @description
* HTTP code snippet generator for Dart http package.
*
* @author
* @AI-Generated
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForSingleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';

export interface DartHttpOptions {
showBoilerplate?: boolean;
checkErrors?: boolean;
printBody?: boolean;
timeout?: number;
insecureSkipVerify?: boolean;
}

export const http: Client<DartHttpOptions> = {
info: {
key: 'http',
title: 'HTTP',
link: 'https://pub.dev/packages/http',
description: 'Dart HTTP client request using the http package',
},
convert: ({ postData, method, allHeaders, fullUrl }, options = {}) => {
const { blank, push, join } = new CodeBuilder({ indent: ' ' });

const {
showBoilerplate = true,
checkErrors = false,
printBody = true,
timeout = -1,
insecureSkipVerify = false,
} = options;

const indent = showBoilerplate ? 1 : 0;

// Create boilerplate
if (showBoilerplate) {
push('import \'package:http/http.dart\' as http;');

blank();
push('void main() async {');
blank();
}

// Create client with timeout if specified
if (timeout > 0) {
push('final client = http.Client();', indent);
push(`client.timeout = Duration(seconds: ${timeout});`, indent);
blank();
}

// Add headers setup
if (Object.keys(allHeaders).length) {
push('final headers = {', indent);
Object.keys(allHeaders).forEach(key => {
push(`'${key}': '${escapeForSingleQuotes(allHeaders[key])}',`, indent + 1);
});
push('};', indent);
blank();
}

// Prepare request
const headersVar = Object.keys(allHeaders).length ? 'headers' : '{}';

if (postData.text) {
push(`final response = await http.${method.toLowerCase()}(`, indent);
push(` Uri.parse('${fullUrl}'),`, indent);
push(` headers: ${headersVar},`, indent);
push(` body: ${JSON.stringify(postData.text)},`, indent);
push(');', indent);
} else {
push(`final response = await http.${method.toLowerCase()}(`, indent);
push(` Uri.parse('${fullUrl}'),`, indent);
push(` headers: ${headersVar},`, indent);
push(');', indent);
}

// Print response
blank();
push('print(response.statusCode);', indent);

if (printBody) {
push('print(response.body);', indent);
}

// End main block
if (showBoilerplate) {
blank();
push('}');
}

return join();
},
};
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/application-form-encoded.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'application/x-www-form-urlencoded',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "foo=bar&hello=world",
);

print(response.statusCode);
print(response.body);

}
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/application-json.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'application/json',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}",
);

print(response.statusCode);
print(response.body);

}
14 changes: 14 additions & 0 deletions src/targets/dart/http/fixtures/boilerplate-option.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
final headers = {
'cookie': 'foo=bar; bar=baz',
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value'),
headers: headers,
body: "foo=bar",
);

print(response.statusCode);
print(response.body);
17 changes: 17 additions & 0 deletions src/targets/dart/http/fixtures/cookies.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'cookie': 'foo=bar; bar=baz',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
);

print(response.statusCode);
print(response.body);

}
13 changes: 13 additions & 0 deletions src/targets/dart/http/fixtures/custom-method.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:http/http.dart' as http;

void main() async {

final response = await http.propfind(
Uri.parse('http://mockbin.com/har'),
headers: {},
);

print(response.statusCode);
print(response.body);

}
20 changes: 20 additions & 0 deletions src/targets/dart/http/fixtures/full.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'cookie': 'foo=bar; bar=baz',
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value'),
headers: headers,
body: "foo=bar",
);

print(response.statusCode);
print(response.body);

}
19 changes: 19 additions & 0 deletions src/targets/dart/http/fixtures/headers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'accept': 'application/json',
'x-foo': 'Bar',
'quoted-value': '"quoted" \'string\'',
};

final response = await http.get(
Uri.parse('http://mockbin.com/har'),
headers: headers,
);

print(response.statusCode);
print(response.body);

}
13 changes: 13 additions & 0 deletions src/targets/dart/http/fixtures/https.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:http/http.dart' as http;

void main() async {

final response = await http.get(
Uri.parse('https://mockbin.com/har'),
headers: {},
);

print(response.statusCode);
print(response.body);

}
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/jsonObj-multiline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'application/json',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "{\n \"foo\": \"bar\"\n}",
);

print(response.statusCode);
print(response.body);

}
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/jsonObj-null-value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'application/json',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "{\"foo\":null}",
);

print(response.statusCode);
print(response.body);

}
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/multipart-data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'multipart/form-data; boundary=---011000010111000001101001',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n",
);

print(response.statusCode);
print(response.body);

}
18 changes: 18 additions & 0 deletions src/targets/dart/http/fixtures/multipart-file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:http/http.dart' as http;

void main() async {

final headers = {
'content-type': 'multipart/form-data; boundary=---011000010111000001101001',
};

final response = await http.post(
Uri.parse('http://mockbin.com/har'),
headers: headers,
body: "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n",
);

print(response.statusCode);
print(response.body);

}
Loading