-
Notifications
You must be signed in to change notification settings - Fork 2
/
HTTPCodeGenerator.js
executable file
·152 lines (139 loc) · 5.84 KB
/
HTTPCodeGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var XojoCodeGenerator = function() {
this.generate = function(context, requests, options) {
for(var i in requests) {
var request = requests[i];
var client_code = [];
client_code[client_code.length] = "// " + request.name;
if (request.description != "") {
client_code[client_code.length] = "// " + request.description;
}
client_code[client_code.length] = "";
client_code[client_code.length] = "// NOTE: Starting with Xojo 2018r4, use of insecure connections";
client_code[client_code.length] = "// in a macOS app requires the addition of the following plist key:";
client_code[client_code.length] = "// <key>NSAppTransportSecurity</key>";
client_code[client_code.length] = "// <dict><key>NSAllowsArbitraryLoads</key><true/></dict>";
client_code[client_code.length] = "";
var vars = request.variables;
if (vars.length > 0) {
client_code[client_code.length] = "// Variable Definitions"
for(i=0;i<vars.length;i++) {
var desc = "// " + vars[i].name + ": " + vars[i].description;
if (vars[i].required) {
desc += " (required)";
}
client_code[client_code.length] = desc;
client_code[client_code.length] = "// " + vars[i].type;
}
client_code[client_code.length] = "";
}
client_code[client_code.length] = "// Set up the socket";
if(request.secure) {
client_code[client_code.length] = "dim h as new HTTPSecureSocket";
client_code[client_code.length] = "h.Secure = True";
client_code[client_code.length] = "h.ConnectionType = h.SSLv23";
client_code[client_code.length] = "";
} else {
client_code[client_code.length] = "dim h as new HTTPSocket";
client_code[client_code.length] = "";
}
var headers = request.headers;
var hasAuth = false
if(JSON.stringify(headers) != "{}") {
client_code[client_code.length] = "// Headers";
for (var headerName in headers) {
var headerValue = headers[headerName];
if(!(request.body != "" && headerName == "Content-Type")) {
client_code[client_code.length] = "h.SetRequestHeader(\"" + headerName + "\",\"" + headerValue + "\")";
}
}
client_code[client_code.length] = "";
}
// Deal with the different body types
var body;
var mimeType;
console.log(request.body);
if(request.body != "") {
client_code[client_code.length] = "// Request Body";
}
// Figure out what kind of body the user specified
if(request.multipartBody) {
body = request.multipartBody;
if(Object.size(body) > 0) {
mimeType = "multipart/form-data";
client_code[client_code.length] = "// Type = Multipart";
client_code[client_code.length] = "Dim sa() As String";
for(var propertyName in body) {
var key = propertyName;
var value = body[key];
client_code[client_code.length] = "sa.append \"" + key + "=\" + EncodeURLComponent(\"" + value + "\")";
}
client_code[client_code.length] = "Dim data as String = Join(sa,\"&\")";
}
} else if(request.jsonBody) {
body = request.jsonBody;
mimeType = "application/json";
client_code[client_code.length] = "// Type = JSON"
var jsontext = JSON.stringify(body).replace(/\"/g,"\"\"");
client_code[client_code.length] = "Dim data as String = \"" + jsontext + "\"";
} else if(request.urlEncodedBody) {
body = request.urlEncodedBody;
if(Object.size(body) > 0) {
mimeType = "application/x-www-form-urlencoded";
client_code[client_code.length] = "// Type = Form URL-Encoded"
client_code[client_code.length] = "Dim sa() as String";
for(var propertyName in body) {
var key = propertyName;
var value = body[key];
client_code[client_code.length] = "sa.append \"" + key + "=\" + EncodeURLComponent(\"" + value + "\")";
}
client_code[client_code.length] = "Dim data as String = Join(sa,\"&\")";
}
} else if(request.body) {
if(request.body.length > 0) {
var replaceCRLF = new RegExp('\n', 'g');
var replaceQuotes = new RegExp('\"', 'g');
body = request.body;
mimeType = "text/plain";
client_code[client_code.length] = "// Type = Text"
client_code[client_code.length] = "Dim data as String = \"" + body.replace(replaceQuotes,"\"\"").replace(replaceCRLF, "\" + EndOfLine + \"") + "\"";
}
}
if(mimeType) {
client_code[client_code.length] = "";
client_code[client_code.length] = "// Assign to the Request's Content";
client_code[client_code.length] = "h.SetRequestContent(data,\"" + mimeType + "\")";
client_code[client_code.length] = "";
}
var paramNames = request.getUrlParametersNames();
var params = request.getUrlParameters();
if(paramNames.length > 0) {
client_code[client_code.length] = "// URL Parameters";
client_code[client_code.length] = "dim parameters() as string";
for(i=0;i<paramNames.length;i++) {
client_code[client_code.length] = "parameters.append \"" + paramNames[i] + "=\" + EncodeURLComponent(\"" + params[paramNames[i]] + "\")";
}
client_code[client_code.length] = "";
}
client_code[client_code.length] = "// Set the URL";
client_code[client_code.length] = "dim url as string = \"" + request.getUrlBase(false) + "\"";
if(paramNames.length > 0) {
client_code[client_code.length] = "url = url + \"?\" + Join(parameters,\"&\")";
}
client_code[client_code.length] = ""
client_code[client_code.length] = "// Send Synchronous Request"
client_code[client_code.length] = "dim s as string = h.SendRequest(\"" + request.method + "\",url,30)";
return client_code.join("\r");
}
}
}
XojoCodeGenerator.identifier = "com.xojo.PawExtensions.HTTPCodeGenerator";
XojoCodeGenerator.title = "Xojo Framework HTTPSocket";
XojoCodeGenerator.fileExtension = "xojo_code";
registerCodeGenerator(XojoCodeGenerator);
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};