Skip to content

Commit bbff9bd

Browse files
authored
Added template for Swift (#1577)
1 parent 4932463 commit bbff9bd

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

src/bindingHandlers/syntaxHighlight.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ko from "knockout";
22
import * as Prism from "prismjs";
3+
import "prismjs/components/prism-markup-templating";
34
import "prismjs/components/prism-javascript";
45
import "prismjs/components/prism-http";
56
import "prismjs/components/prism-c";
@@ -10,7 +11,8 @@ import "prismjs/components/prism-ruby";
1011
import "prismjs/components/prism-markup";
1112
import "prismjs/components/prism-bash";
1213
import "prismjs/components/prism-json";
13-
// import "prismjs/components/prism-php"; // broken!
14+
import "prismjs/components/prism-swift";
15+
import "prismjs/components/prism-php";
1416

1517

1618
interface SyntaxHighlightConfig {
@@ -44,15 +46,17 @@ ko.bindingHandlers["syntaxHighlight"] = {
4446
highlightLanguage = "js";
4547
break;
4648
case "php":
47-
// highlightLanguage = "php"; // broken!
48-
highlightLanguage = "ruby";
49+
highlightLanguage = "php";
4950
break;
5051
case "python":
5152
highlightLanguage = "python";
5253
break;
5354
case "ruby":
5455
highlightLanguage = "ruby";
5556
break;
57+
case "swift":
58+
highlightLanguage = "swift"
59+
break;
5660
case "xml":
5761
highlightLanguage = "xml";
5862
break;

src/components/operations/operation-details/ko/runtime/operation-console.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export class OperationConsole {
254254
{ value: "php", text: "PHP" },
255255
{ value: "python", text: "Python" },
256256
{ value: "ruby", text: "Ruby" },
257+
{ value: "swift", text: "Swift" },
257258
];
258259
}
259260

src/components/operations/operation-details/ko/runtime/templates/php.liquid

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ curl_setopt($curl, CURLOPT_URL, $url);
88
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
99

1010
{% if request.meaningfulHeaders.size > 0 -%}
11-
// Request headers
11+
# Request headers
1212
$headers = array(
1313
{%- for header in request.meaningfulHeaders %}
1414
'{{header.name}}: {{header.displayedValue}}',
1515
{%- endfor -%});
1616
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
1717
{%- endif %}
1818
{% if request.body != blank %}
19-
// Request body
19+
# Request body
2020
{% if request.bodyFormat == "raw" -%}
2121
$request_body = '{{request.body}}';
2222
{%- elsif request.bodyFormat == "binary" %}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Foundation
2+
import FoundationNetworking
3+
4+
let url = URL(string: "{{requestUrl}}")
5+
var request : URLRequest = URLRequest(url: url!)
6+
request.httpMethod = "{{method}}"
7+
8+
// Request headers
9+
let headers = [
10+
{% for header in request.meaningfulHeaders %}
11+
"{{header.name}}": "{{header.displayedValue}}",
12+
{%- endfor %}
13+
]
14+
request.allHTTPHeaderFields = headers
15+
{% if request.body != blank %}
16+
// Request body
17+
{%- if request.bodyFormat == "raw" -%}
18+
{%- assign formattedBody = request.body | replace:'"','\\"'
19+
formattedBody = formattedBody | replace: '\r\n', ' '
20+
formattedBody = formattedBody | replace: ' ', ' ' %}
21+
let postString = "{{formattedBody}}"
22+
request.httpBody = postString.data(using: .utf8)
23+
{%- elsif request.bodyFormat == "binary" %}
24+
let file = "< path/to/{{request.binary.name}} >"
25+
var result = ""
26+
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
27+
let fileURL = dir.appendingPathComponent(file)
28+
do {
29+
result = try String(contentsOf: fileURL, encoding: .utf8)
30+
}
31+
catch {
32+
print("error")
33+
}
34+
request.httpBody = result.data(using: .utf8)
35+
}
36+
{% endif %}
37+
{% endif %}
38+
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
39+
if (error != nil) {
40+
if let error = error as Error? {
41+
print(error)
42+
}
43+
} else {
44+
if let response = response as? HTTPURLResponse, let responseCode = response.statusCode as Int? {
45+
print(responseCode)
46+
}
47+
if let data = data, let dataString = String(data: data, encoding: .utf8) {
48+
print(dataString)
49+
}
50+
}
51+
}
52+
task.resume()
53+
54+
RunLoop.main.run()

src/components/operations/operation-details/ko/runtime/templates/templates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as javascript from "./javascript.liquid";
66
import * as php from "./php.liquid";
77
import * as python from "./python.liquid";
88
import * as ruby from "./ruby.liquid";
9-
9+
import * as swift from "./swift.liquid"
1010
import * as ws_wscat from "./ws_wscat.liquid";
1111
import * as ws_csharp from "./ws_csharp.liquid";
1212
import * as ws_javascript from "./ws_javascript.liquid";
@@ -20,6 +20,7 @@ export const templates = {
2020
php: php.default,
2121
python: python.default,
2222
ruby: ruby.default,
23+
swift: swift.default,
2324
ws_wscat: ws_wscat.default,
2425
ws_csharp: ws_csharp.default,
2526
ws_javascript: ws_javascript.default,

0 commit comments

Comments
 (0)