-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.html
390 lines (343 loc) · 14.4 KB
/
index.html
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=https://www.hackerearth.com/docs/api/developers/code/legacy/">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>developer.hackerearth.com by HackerEarth</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- javascript for google analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-38522368-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="container">
<div class="masthead">
<h3 class="muted pull-left">developer.hackerearth.com</h3>
<h3 class="muted pull-right">API v2</h3>
<div style="clear:both;"></div>
<hr>
</div>
<h3>The HackerEarth API</h3>
<p>This is HackerEarth API resource documentation. If you have any
problems or requests please contact the <a
href="mailto:praveen@hackerearth.com"
target="_blank">maintainer</a>.</p>
<br>
<p>The API provides endpoints for compiling and running code in
several languages. It can be accessed via a simple API key based
authorization process. You need to <a
href="http://www.hackerearth.com/api/register/"
target="_blank">register your web application</a> in
order to get a client specific client_secret which must be provided
while communicating with the API. Currently, there is a cap on the
number of clients a user can register and also on the number of
free requests a user can make. The API receives all the data via
POST and returns the data as JSON. You can register your own client
<a href="http://www.hackerearth.com/api/register/"
target="_blank">here</a>.</p>
<br>
<p>The API defines the following endpoints:</p>
<pre>
http://api.hackerearth.com/code/compile/
http://api.hackerearth.com/code/run/
</pre>
<br>
<p>Every JSON object returned by the API always consists of atleast
two attributes, 'message' and 'errors'. 'message' contains the
description of errors encountered while processing of a request and
'errors' may contain key-value pairs which were the cause of the
errors.</p>
<pre>
{"message": "ArgumentMissingError: client_secret is needed!", "errors": {}}
</pre>
<br>
<p>In case of a successful compile request, the JSON object
returned looks like:</p>
<pre>
{"errors": {},
"id": "7e5cfbe",
"code_id": "7e5cfbe",
"message": "OK",
"compile_status": "OK",
"web_link": "http://code.hackerearth.com/7e5cfbe"}
</pre>
<br>
<p>In case of a successful run request, the JSON object returned
might look like:</p>
<pre>
{"errors": {},
"id": "7e5cfbe",
"code_id": "7e5cfbe",
"message": "OK",
"compile_status": "OK",
"run_status": {"status": "AC",
"time_used": "0.1005",
"memory_used": "64",
"output": "Hello",
"output_html": "Hello",
"signal": "OTHER",
"status_detail": "N/A",
"time_limit": 5,
"memory_limit": 262144}
"web_link": "http://code.hackerearth.com/7e5cfbe"}
</pre>
<br>
<p>Each endpoint needs atleast three necessary parameters viz
client_secret, source and lang.</p>
<h4>client_secret</h4>
<p>client_secret is a 40 character random key that serves as an
identification for a particular client and must be provided when
communicating with the API. You can create a client_secret on the API
registration page. Failing to provide a client_secret returns the
following 400 Bad Request response.</p>
<pre>
>> curl -i http://api.hackerearth.com/code/compile/
>>
HTTP/1.1 400 BAD REQUEST
Date: Wed, 23 Jan 2013 22:56:25 GMT
Server: Apache/2.2.22 (Ubuntu)
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json
{"message": "ArgumentMissingError: client_secret is needed!", "errors": {}}
</pre>
<br>
<p>Providing an invalid client_secret returns the following 400 Bad
Request response. The invalid client_secret is also returned in
'errors' attribute of the JSON response object.<p>
<pre>
>> curl -i --data "client_secret=adsafwqf5dcb5757e72c96d9" http://api.hackerearth.com/code/compile/
>>
HTTP/1.1 400 BAD REQUEST
Date: Wed, 23 Jan 2013 23:00:20 GMT
Server: Apache/2.2.22 (Ubuntu)
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json
{"message": "UnregisteredClientError: Client does not exist", "errors": {"client_secret": "adsafwqf5dcb5757e72c96d969a1a"}}
</pre>
<br>
<h4>source</h4>
<p>source is the actual text or the source code that you want to
compile or run. Failing to provide the source parameter returns 400
response.</p>
<pre>
>> curl -i --data "client_secret=5db3f1c12c59caa1002d1cb5" http://api.hackerearth.com/code/compile/
>>
HTTP/1.1 400 BAD REQUEST
Date: Thu, 24 Jan 2013 16:38:09 GMT
Server: Apache/2.2.22 (Ubuntu)
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json
{"message": "ArgumentMissingError: source is needed!", "errors": {}}
</pre>
<br>
<h4>lang</h4>
<p>lang is a string representing the programming language in which the
source is written. lang is a required parameter and a similar response
as that of source is returned by the API if lang is missing in the POST
requests.
<br>
Possible values of lang argument are C, CPP, CPP11, CLOJURE, CSHARP, JAVA,
JAVASCRIPT, HASKELL, PERL, PHP, PYTHON, RUBY. The corresponding langauges are
C, C++, C++11, Clojure, C#, Java, JavaScript, Haskell, Perl, PHP, Python and
Ruby.
</p>
<br>
<h4>input</h4>
<p>input is an optional parameter which represents the input to be
fed to your program.</p>
<br>
<h4>time_limit</h4>
<p>time_limit is an optional parameter which is the max time for
which program is allowed to run. Maximum value of time limit can be
5 seconds. Any value greater than 5 in the API request is set to 5.
Exceeding the time limit returns the status as <i>TLE</i> in the
response.</p>
<br>
<h4>memory_limit</h4>
<p> memory_limit is an optional paramer which is the max memory
allowed to be used by the program. Maximum value of memory limit
can be 262144 (256 MB). Any value greater than 262144 in the API
request is set to
262144. Exceeding the memory limit returns the status as <i>MLE</i>
in the response.<p>
<br>
<h4>async</h4>
<p>async is an optional parameter which is the API request mode
used by the program. The value 1 turns it on. You should use async
mode while writing applications so that you can avoid processes from
hanging for response and reduce memory overhead and timeout.
<br>
<p>Below is a sample python script which makes valid requests to
the HackerEarth API.</p>
<br>
<h4>Synchronous API request from raw string</h4>
<pre>
#! -*- coding: utf-8 -*-
import requests
# constants
RUN_URL = u'http://api.hackerearth.com/code/run/'
CLIENT_SECRET = '5db3f1c12c59caa1002d1cb5757e72c96d969a1a'
source = "import sys;print(sys.path)"
data = {
'client_secret': CLIENT_SECRET,
'async': 0,
'source': source,
'lang': "PYTHON",
'time_limit': 5,
'memory_limit': 262144,
}
r = requests.post(RUN_URL, data=data)
print r.json()
</pre>
<br>
<p>Output from the above script is:</p>
<pre>
post_data: lang=PYTHON&time_limit=5&memory_limit=262144&source=import+sys%3Bprint%28sys.path%29&
async=0&client_secret=5db3f1c12c59caa1002d1cb5757e72c96d969a1a
{u'errors': {}, u'code_id': u'26c372Q', u'web_link': u'http://code.hackerearth.com/26c372Q', u'compile_status': u'OK',
u'message': u'OK', u'run_status': {u'status': u'AC', u'time_limit': 5,
u'output_html': u"['/tmp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages',
'/usr/lib/pymodules/python2.7']<br>",
u'memory_limit': 262144, u'time_used': u'0.1005', u'signal': u'OTHER', u'status_detail': u'N/A', u'async': 0,
u'output': u"['/tmp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']\n", u'memory_used': u'64'}}
</pre>
<br>
<h4>Synchronous API request from file</h4>
<pre>
#! -*- coding: utf-8 -*-
import requests
# constants
RUN_URL = u'http://api.hackerearth.com/code/run/'
CLIENT_SECRET = '5db3f1c12c59caa1002d1cb5757e72c96d969a1a'
source = open('test.py', 'r')
"""
test.py
#! -*- coding: utf-8 -*-
def square(no):
return no * no
print(square(-23))
"""
data = {
'client_secret': CLIENT_SECRET,
'async': 0,
'source': source.read(),
'lang': "PYTHON",
'time_limit': 5,
'memory_limit': 262144,
}
r = requests.post(RUN_URL, data=data)
source.close()
print r.json()
</pre>
<br>
<p>Output from the above script is:</p>
<pre>
post_data: lang=PYTHON&time_limit=5&memory_limit=262144&
source=%23%21+-%2A-+coding%3A+utf-8+-%2A-%0A%0Adef+square%28no%29%3A%0A++++return+no+%2A+no%0A%0Aprint%28square%28-23%29%29%0A%0A&
async=0&client_secret=5db3f1c12c59caa1002d1cb5757e72c96d969a1a
{u'errors': {}, u'code_id': u'c93d09V', u'web_link': u'http://code.hackerearth.com/c93d09V', u'compile_status': u'OK',
u'message': u'OK', u'run_status': {u'status': u'AC', u'time_limit': 5, u'output_html': u'529<br>', u'memory_limit': 262144,
u'time_used': u'0.1005', u'signal': u'OTHER', u'status_detail': u'N/A', u'async': 0, u'output': u'529\n',
u'memory_used': u'64'}}
</pre>
<br>
<h4>Asynchronous API request</h4>
<pre>
import urllib
COMPILE_URL = 'http://api.hackerearth.com/code/compile/'
RUN_URL = 'http://api.hackerearth.com/code/run/'
CLIENT_SECRET = '5db3f1c12c59caa1002d1cb5757e72c96d969a1a'
source = open('sample.c').read()
lang = 'C'
post_data = {
'client_secret': CLIENT_SECRET,
# Asynchronous mode on
'async': 1,
'source': source,
'lang': lang,
'time_limit': 5,
'memory_limit': 262144,
# Id to keep track of request
'id': 123,
# Callback URL where processed response will later arrive
# - this response is same as the response received in
# synchronous API request.
'callback': 'http://example.com/receive-hackerearth-response/'
}
post_data = urllib.urlencode(post_data)
response = urllib.urlopen(RUN_URL, post_data)
print "post_data: ",post_data
print
print response.read()
</pre>
<br>
<p>Output from the above script is:</p>
<pre>
post_data: lang=C&source=%23include+%3Cstdio.h%3E%0A%0Aint+main%28%29+%7B%0A++++printf%28%22Hello%22%29%3B%0A++++int+n%3B%0A++++scanf%28%22%25d%22%2C+%26n%29%3B%0A++++printf%28%22%5Cn%25d%5Cn%22%2C+n%29%3B%0A++++return+0%3B%0A%7D%0A&callback=http%3A%2F%2Fexample.com%2Freceive-hackerearth-response%2F&async=1&time_limit=5&client_secret=4df77c2c2eb62f9adb20bd1127f6f44a4ce6cda4&id=123&memory_limit=262144
{"errors": {}, "code_id": "3d255bX", "id": 123, "message": "OK", "run_status": {"status": "NA", "time_limit": 5, "async": 1, "memory_limit": 262144}, "compile_status": "Compiling...", "web_link": "http://code.hackerearth.com/3d255bX"}
</pre>
<br>
The response recieved at the callback url is in JSON format. You can process it as given in the example code below.
<br>
<br>
<pre>
import json
def api_response(request):
payload = request.POST.get('payload', '')
"""
This payload is in JSON format. You need to load it using
native method to convert it into dictionary for easy operations
later on.
"""
payload = json.loads(payload)
print payload
"""
Payload Format:
{u'errors': {}, u'code_id': u'4c384aM', u'web_link': u'http://code.hackerearth.com/4c384aM', u'compile_status': u'OK', u'id': u'123456', u'async': 1, u'run_status': {u'status': u'AC', u'memory_used': u'64', u'output_html': u'Hello<br>1<br>', u'time_used': u'0.1006', u'signal': u'OTHER', u'status_detail': u'N/A', u'output': u'Hello\\n1\\n'}, u'message': u'OK'}
"""
run_status = payload.get('run_status')
o = run_status['output']
print o
"""
Output:
Hello
1
"""
return HttpResponse('API Response Recieved!')
</pre>
<br>
<p>All submissions are welcome. Sample code for API request in different languages
are welcome. To submit a change, fork this <a
href="https://github.com/HackerEarth/developer.hackerearth.com"
target="_blank">repo</a>, commit your
changes, and send us a pull request.</p>
<hr>
<div class="footer">
<p>This project is maintained by <a
href="https://github.com/HackerEarth">HackerEarth</a>. For any
queries, <a href="mailto:praveen@hackerearth.com"
target="_blank">contact me</a>.</p>
</div>
</div>
</body>
</html>