Skip to content

Commit b9e1d7b

Browse files
yaneccfilfreire
andauthored
Generate Crystal language code (Kong#343)
* Generate Crystal language code * Generate Crystal language code * Remove a blank line * fix crystal tests --------- Co-authored-by: Filipe Freire <[email protected]>
1 parent 893da8b commit b9e1d7b

26 files changed

+280
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![version][npm-version]][npm-url] [![License][npm-license]][license-url]
44

5-
> HTTP Request snippet generator for _many_ languages & tools including: `cURL`, `HTTPie`, `JavaScript`, `Node`, `C`, `Java`, `PHP`, `Objective-C`, `Swift`, `Python`, `Ruby`, `C#`, `Go`, `OCaml` and [more](https://github.com/Kong/httpsnippet/wiki/Targets)!
5+
> HTTP Request snippet generator for _many_ languages & tools including: `cURL`, `HTTPie`, `JavaScript`, `Node`, `C`, `Java`, `PHP`, `Objective-C`, `Swift`, `Python`, `Ruby`, `C#`, `Go`, `OCaml`, `Crystal` and [more](https://github.com/Kong/httpsnippet/wiki/Targets)!
66
77
Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format to import data and describe HTTP calls.
88

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"keywords": [
1212
"api",
1313
"clojure",
14+
"crystal",
1415
"csharp",
1516
"curl",
1617
"go",

src/helpers/__snapshots__/utils.test.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ Array [
3030
"key": "clojure",
3131
"title": "Clojure",
3232
},
33+
Object {
34+
"clients": Array [
35+
Object {
36+
"description": "Crystal HTTP client",
37+
"key": "native",
38+
"link": "https://crystal-lang.org/api/master/HTTP/Client.html",
39+
"title": "http::client",
40+
},
41+
],
42+
"default": "native",
43+
"extname": ".cr",
44+
"key": "crystal",
45+
"title": "Crystal",
46+
},
3347
Object {
3448
"clients": Array [
3549
Object {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import https from '../../../fixtures/requests/https.json';
2+
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures';
3+
import { Request } from '../../../httpsnippet';
4+
5+
runCustomFixtures({
6+
targetId: 'crystal',
7+
clientId: 'native',
8+
tests: [
9+
{
10+
it: 'should support the insecureSkipVerify option',
11+
input: https as Request,
12+
options: {
13+
insecureSkipVerify: true,
14+
},
15+
expected: 'insecure-skip-verify.cr',
16+
},
17+
],
18+
});

src/targets/crystal/native/client.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for native Crystal
4+
*
5+
* @author
6+
* @18183883296
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
import { CodeBuilder } from '../../../helpers/code-builder';
11+
import { escapeForDoubleQuotes } from '../../../helpers/escape';
12+
import { Client } from '../../targets';
13+
14+
export interface CrystalNativeOptions {
15+
insecureSkipVerify?: boolean;
16+
}
17+
18+
export const native: Client<CrystalNativeOptions> = {
19+
info: {
20+
key: 'native',
21+
title: 'http::client',
22+
link: 'https://crystal-lang.org/api/master/HTTP/Client.html',
23+
description: 'Crystal HTTP client',
24+
},
25+
convert: ({ method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
26+
const { insecureSkipVerify = false } = options;
27+
28+
const { push, blank, join } = new CodeBuilder();
29+
30+
push('require "http/client"');
31+
32+
blank();
33+
34+
push(`url = "${fullUrl}"`);
35+
36+
const headers = Object.keys(allHeaders);
37+
if (headers.length) {
38+
push('headers = HTTP::Headers{');
39+
headers.forEach(key => {
40+
push(` "${key}" => "${escapeForDoubleQuotes(allHeaders[key])}"`);
41+
});
42+
push('}');
43+
}
44+
45+
if (postData.text) {
46+
push(`reqBody = ${JSON.stringify(postData.text)}`);
47+
}
48+
49+
blank();
50+
51+
const method = rawMethod.toUpperCase();
52+
const methods = ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT', 'OPTIONS'];
53+
54+
const headersContext = headers.length ? ', headers: headers' : '';
55+
const bodyContext = postData.text ? ', body: reqBody' : '';
56+
const sslContext = insecureSkipVerify ? ', tls: OpenSSL::SSL::Context::Client.insecure' : '';
57+
58+
if (methods.includes(method)) {
59+
push(
60+
`response = HTTP::Client.${method.toLowerCase()} url${headersContext}${bodyContext}${sslContext}`,
61+
);
62+
} else {
63+
push(
64+
`response = HTTP::Client.exec "${method}", url${headersContext}${bodyContext}${sslContext}`,
65+
);
66+
}
67+
68+
push('puts response.body');
69+
70+
return join();
71+
},
72+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"content-type" => "application/x-www-form-urlencoded"
6+
}
7+
reqBody = "foo=bar&hello=world"
8+
9+
response = HTTP::Client.post url, headers: headers, body: reqBody
10+
puts response.body
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"content-type" => "application/json"
6+
}
7+
reqBody = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
8+
9+
response = HTTP::Client.post url, headers: headers, body: reqBody
10+
puts response.body
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"cookie" => "foo=bar; bar=baz"
6+
}
7+
8+
response = HTTP::Client.post url, headers: headers
9+
puts response.body
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
5+
response = HTTP::Client.exec "PROPFIND", url
6+
puts response.body
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
4+
headers = HTTP::Headers{
5+
"cookie" => "foo=bar; bar=baz"
6+
"accept" => "application/json"
7+
"content-type" => "application/x-www-form-urlencoded"
8+
}
9+
reqBody = "foo=bar"
10+
11+
response = HTTP::Client.post url, headers: headers, body: reqBody
12+
puts response.body

0 commit comments

Comments
 (0)