-
Notifications
You must be signed in to change notification settings - Fork 37
/
_main.mako
187 lines (163 loc) · 4.12 KB
/
_main.mako
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<%def name="recursive_expand(dikt, delimiter='\\')">
<%
if not dikt:
raise StopIteration
keys = sorted(dikt.keys(), key=lambda x: isinstance(dikt[x], dict))
try:
last = keys[-1]
except IndexError:
last = keys[0]
lines = []
for k in keys:
if isinstance(dikt[k], dict):
for subk, v, slash in recursive_expand(dikt[k], delimiter):
lines.append(('{0}[{1}]'.format(k, subk), v, slash))
else:
lines.append((k, dikt[k], '' if k is last else delimiter))
return lines
%>
</%def>
## http://stackoverflow.com/a/6701741/133514
<%def name="route_for_endpoint(endpoint, select=None)">
<%
return context['Endpoint'](ctx, endpoint, select)
%>
</%def>
<%def name="interpolate_uri(uri, **kwargs)">
<%
# this basically parses :bank_account_id to a string with {bank_account_id}
# for much easier interpolation.
import re
# note that it shouldn't start with a digit!
_uri = re.sub('(:([^\d]\w+))+', r'{\2}', uri)
return _uri.format(**kwargs)
%>
</%def>
<%def name="make_endpoint(endpoint_name, select=None)">
<%
return context['Endpoint'](ctx, endpoint_name, select)
%>
</%def>
<%def name="php_boilerplate()">
## i'm putting this between an interpolation because pycharm's introspection
## correctly detects that this is an unclosed tag and warns me..annoying.
${"<?php"}
require(__DIR__ . '/vendor/autoload.php');
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
%if api_location:
Balanced\Settings::configure("${api_location}", "${ctx.api_key}");
%else:
Balanced\Settings::$api_key = "${ctx.api_key}";
%endif
</%def>
<%def name="python_boilerplate()">
import balanced
%if api_location:
balanced.config.root_uri = "${api_location}"
%endif
balanced.configure("${ctx.api_key}")
</%def>
<%def name="ruby_boilerplate()">
require 'balanced'
%if api_location:
<%
import urlparse
parsed_url = urlparse.urlparse(api_location)
_root_url = parsed_url.netloc
if ':' in _root_url:
_root_url, _, _ = parsed_url.netloc.partition(':')
%>
options = {
:scheme => 'http',
:host => '${_root_url}',
:port => 5000,
}
Balanced.configure('${ctx.api_key}', options)
%else:
Balanced.configure('${ctx.api_key}')
%endif
</%def>
<%def name="curl_show_template(endpoint_name)">
% if mode == 'definition':
<%
ep = Endpoint(ctx, endpoint_name, select='shortest')
%>
${ep.method} ${ep.url}
% else:
<%
slash = '\\'
%>
curl ${Endpoint.qualify_uri(ctx, request['uri'])} ${slash}
-u ${ctx.api_key}:
% endif
</%def>
<%def name="curl_create_template(endpoint_name, uri='uri', ep=None)">
<%
ep = Endpoint(ctx, endpoint_name, select='shortest')
%>
%if mode == 'definition':
${ep.method} ${ep.url}
%elif mode == 'request':
<%
slash = '\\'
%>
curl ${Endpoint.qualify_uri(ctx, request[uri])} ${slash}
-u ${ctx.api_key}: ${slash}
%if 'payload' in request:
%for k, v, slash in recursive_expand(request['payload']):
-d "${k}=${v}" ${slash}
%endfor
%else:
-X POST
%endif
%endif
</%def>
<%def name="curl_update_template(endpoint_name, uri='uri', ep=None)">
<%
ep = Endpoint(ctx, endpoint_name, select='shortest')
%>
%if mode == 'definition':
${ep.method} ${ep.url}
%elif mode == 'request':
<%
slash = '\\'
%>
curl ${Endpoint.qualify_uri(ctx, request[uri])} ${slash}
-u ${ctx.api_key}: ${slash}
-X PUT ${slash}
%for k, v, slash in recursive_expand(request['payload']):
-d "${k}=${v}" ${slash}
%endfor
%endif
</%def>
<%def name="curl_list_template(endpoint_name, limit=2, ep=None)">
<%
ep = Endpoint(ctx, endpoint_name, select='shortest')
%>
% if request == 'definition':
${ep.method} ${ep.url}
%elif mode == 'request':
<%
slash = '\\'
%>
curl ${Endpoint.qualify_uri(ctx, request['uri'], limit=limit)} ${slash}
-u ${ctx.api_key}:
% endif
</%def>
<%def name="curl_delete_template(endpoint_name, ep=None)">
<%
ep = Endpoint(ctx, endpoint_name)
%>
% if mode == 'definition':
${ep.method} ${ep.url}
% else:
<%
slash = '\\'
%>
curl ${Endpoint.qualify_uri(ctx, request['uri'])} ${slash}
-u ${ctx.api_key}: ${slash}
-X DELETE
% endif
</%def>