examples: annotate examples
[nit.git] / lib / curl / examples / curl_http.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Example use of the Curl module
18 module curl_http is example
19
20 import curl
21
22 # Custom delegate to receive callbacks from a Curl transfer
23 class MyHttpFetcher
24 super CurlCallbacks
25
26 # Body of the downloaded file
27 var fetched_body = ""
28
29 redef fun header_callback(line) do
30 # We keep this callback silent for testing purposes
31 #if not line.has_prefix("Date:") then print "Header_callback: {line}"
32 end
33
34 redef fun body_callback(line) do self.fetched_body += line
35
36 redef fun stream_callback(buffer) do print "Stream_callback: {buffer}"
37 end
38
39 private fun print_usage do print "Usage: curl_http [POST|GET|GET_FILE] url"
40
41 if args.length < 2 then
42 print_usage
43 exit 1
44 end
45
46 var url = args[1]
47 var request = new CurlHTTPRequest(url)
48 request.verbose = false # Set to `true` to debug
49
50 if args[0] == "GET" then
51 # HTTP Get Request
52 var response = request.execute
53
54 if response isa CurlResponseSuccess then
55 print "Status code: {response.status_code}"
56 print "Body: {response.body_str}"
57 else if response isa CurlResponseFailed then
58 print "Error code: {response.error_code}"
59 print "Error msg: {response.error_msg}"
60 end
61
62 else if args[0] == "POST" then
63 # HTTP Post Request
64 var my_http_fetcher = new MyHttpFetcher
65 request.delegate = my_http_fetcher
66
67 var post_data = new HeaderMap
68 post_data["Bugs Bunny"] = "Daffy Duck"
69 post_data["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*"
70 post_data["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only one, the last one"
71 request.data = post_data
72 var response = request.execute
73
74 print "Our body from the callback: {my_http_fetcher.fetched_body}"
75
76 if response isa CurlResponseSuccess then
77 print "*** Answer ***"
78 print "Status code: {response.status_code}"
79 print "Body should be empty, because we decided to manage callbacks: {response.body_str.length}"
80 else if response isa CurlResponseFailed then
81 print "Error code: {response.error_code}"
82 print "Error msg: {response.error_msg}"
83 end
84
85 else if args[0] == "GET_FILE" then
86 # HTTP Get to file Request
87 var headers = new HeaderMap
88 headers["Accept"] = "Moo"
89 request.headers = headers
90 var response = request.download_to_file(null)
91
92 if response isa CurlFileResponseSuccess then
93 print "*** Answer ***"
94 print "Status code: {response.status_code}"
95 print "Size downloaded: {response.size_download}"
96 else if response isa CurlResponseFailed then
97 print "Error code: {response.error_code}"
98 print "Error msg: {response.error_msg}"
99 end
100
101 else
102 print_usage
103 exit 1
104 end