-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Crystal language code (#343)
* Generate Crystal language code * Generate Crystal language code * Remove a blank line * fix crystal tests --------- Co-authored-by: Filipe Freire <livrofubia@gmail.com>
- Loading branch information
Showing
26 changed files
with
280 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"keywords": [ | ||
"api", | ||
"clojure", | ||
"crystal", | ||
"csharp", | ||
"curl", | ||
"go", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import https from '../../../fixtures/requests/https.json'; | ||
import { runCustomFixtures } from '../../../fixtures/runCustomFixtures'; | ||
import { Request } from '../../../httpsnippet'; | ||
|
||
runCustomFixtures({ | ||
targetId: 'crystal', | ||
clientId: 'native', | ||
tests: [ | ||
{ | ||
it: 'should support the insecureSkipVerify option', | ||
input: https as Request, | ||
options: { | ||
insecureSkipVerify: true, | ||
}, | ||
expected: 'insecure-skip-verify.cr', | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @description | ||
* HTTP code snippet generator for native Crystal | ||
* | ||
* @author | ||
* @18183883296 | ||
* | ||
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. | ||
*/ | ||
import { CodeBuilder } from '../../../helpers/code-builder'; | ||
import { escapeForDoubleQuotes } from '../../../helpers/escape'; | ||
import { Client } from '../../targets'; | ||
|
||
export interface CrystalNativeOptions { | ||
insecureSkipVerify?: boolean; | ||
} | ||
|
||
export const native: Client<CrystalNativeOptions> = { | ||
info: { | ||
key: 'native', | ||
title: 'http::client', | ||
link: 'https://crystal-lang.org/api/master/HTTP/Client.html', | ||
description: 'Crystal HTTP client', | ||
}, | ||
convert: ({ method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => { | ||
const { insecureSkipVerify = false } = options; | ||
|
||
const { push, blank, join } = new CodeBuilder(); | ||
|
||
push('require "http/client"'); | ||
|
||
blank(); | ||
|
||
push(`url = "${fullUrl}"`); | ||
|
||
const headers = Object.keys(allHeaders); | ||
if (headers.length) { | ||
push('headers = HTTP::Headers{'); | ||
headers.forEach(key => { | ||
push(` "${key}" => "${escapeForDoubleQuotes(allHeaders[key])}"`); | ||
}); | ||
push('}'); | ||
} | ||
|
||
if (postData.text) { | ||
push(`reqBody = ${JSON.stringify(postData.text)}`); | ||
} | ||
|
||
blank(); | ||
|
||
const method = rawMethod.toUpperCase(); | ||
const methods = ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT', 'OPTIONS']; | ||
|
||
const headersContext = headers.length ? ', headers: headers' : ''; | ||
const bodyContext = postData.text ? ', body: reqBody' : ''; | ||
const sslContext = insecureSkipVerify ? ', tls: OpenSSL::SSL::Context::Client.insecure' : ''; | ||
|
||
if (methods.includes(method)) { | ||
push( | ||
`response = HTTP::Client.${method.toLowerCase()} url${headersContext}${bodyContext}${sslContext}`, | ||
); | ||
} else { | ||
push( | ||
`response = HTTP::Client.exec "${method}", url${headersContext}${bodyContext}${sslContext}`, | ||
); | ||
} | ||
|
||
push('puts response.body'); | ||
|
||
return join(); | ||
}, | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/targets/crystal/native/fixtures/application-form-encoded.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "application/x-www-form-urlencoded" | ||
} | ||
reqBody = "foo=bar&hello=world" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "application/json" | ||
} | ||
reqBody = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"cookie" => "foo=bar; bar=baz" | ||
} | ||
|
||
response = HTTP::Client.post url, headers: headers | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
|
||
response = HTTP::Client.exec "PROPFIND", url | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" | ||
headers = HTTP::Headers{ | ||
"cookie" => "foo=bar; bar=baz" | ||
"accept" => "application/json" | ||
"content-type" => "application/x-www-form-urlencoded" | ||
} | ||
reqBody = "foo=bar" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"accept" => "application/json" | ||
"x-foo" => "Bar" | ||
"quoted-value" => "\"quoted\" 'string'" | ||
} | ||
|
||
response = HTTP::Client.get url, headers: headers | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "https://mockbin.com/har" | ||
|
||
response = HTTP::Client.get url | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "https://mockbin.com/har" | ||
|
||
response = HTTP::Client.get url, tls: OpenSSL::SSL::Context::Client.insecure | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "application/json" | ||
} | ||
reqBody = "{\n \"foo\": \"bar\"\n}" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "application/json" | ||
} | ||
reqBody = "{\"foo\":null}" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "multipart/form-data; boundary=---011000010111000001101001" | ||
} | ||
reqBody = "-----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" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "multipart/form-data; boundary=---011000010111000001101001" | ||
} | ||
reqBody = "-----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" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
9 changes: 9 additions & 0 deletions
9
src/targets/crystal/native/fixtures/multipart-form-data-no-params.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"Content-Type" => "multipart/form-data" | ||
} | ||
|
||
response = HTTP::Client.post url, headers: headers | ||
puts response.body |
10 changes: 10 additions & 0 deletions
10
src/targets/crystal/native/fixtures/multipart-form-data.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"Content-Type" => "multipart/form-data; boundary=---011000010111000001101001" | ||
} | ||
reqBody = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value" | ||
|
||
response = HTTP::Client.get url | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" | ||
|
||
response = HTTP::Client.get url | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
|
||
response = HTTP::Client.get url | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "http/client" | ||
|
||
url = "http://mockbin.com/har" | ||
headers = HTTP::Headers{ | ||
"content-type" => "text/plain" | ||
} | ||
reqBody = "Hello World" | ||
|
||
response = HTTP::Client.post url, headers: headers, body: reqBody | ||
puts response.body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Target } from '../targets'; | ||
import { native } from './native/client'; | ||
|
||
export const crystal: Target = { | ||
info: { | ||
key: 'crystal', | ||
title: 'Crystal', | ||
extname: '.cr', | ||
default: 'native', | ||
}, | ||
clientsById: { | ||
native, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters