lib: move some examples/* into specific subdirectories of their lib
[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 # Sample of the Curl module.
18 module curl_http
19
20 import curl
21
22 # Small class to represent an Http Fetcher
23 class MyHttpFetcher
24 super CurlCallbacks
25
26 var curl: Curl
27 var our_body: String = ""
28
29 init(curl: Curl) do self.curl = curl
30
31 # Release curl object
32 fun destroy do self.curl.destroy
33
34 # Header callback
35 redef fun header_callback(line: String) do
36 # We keep this callback silent for testing purposes
37 #if not line.has_prefix("Date:") then print "Header_callback : {line}"
38 end
39
40 # Body callback
41 redef fun body_callback(line: String) do self.our_body = "{self.our_body}{line}"
42
43 # Stream callback - Cf : No one is registered
44 redef fun stream_callback(buffer: String, size: Int, count: Int) do print "Stream_callback : {buffer} - {size} - {count}"
45 end
46
47
48 # Program
49 if args.length < 2 then
50 print "Usage: curl_http <method wished [POST, GET, GET_FILE]> <target url>"
51 else
52 var curl = new Curl
53 var url = args[1]
54 var request = new CurlHTTPRequest(url, curl)
55
56 # HTTP Get Request
57 if args[0] == "GET" then
58 request.verbose = false
59 var getResponse = request.execute
60
61 if getResponse isa CurlResponseSuccess then
62 print "Status code : {getResponse.status_code}"
63 print "Body : {getResponse.body_str}"
64 else if getResponse isa CurlResponseFailed then
65 print "Error code : {getResponse.error_code}"
66 print "Error msg : {getResponse.error_msg}"
67 end
68
69 # HTTP Post Request
70 else if args[0] == "POST" then
71 var myHttpFetcher = new MyHttpFetcher(curl)
72 request.delegate = myHttpFetcher
73
74 var postDatas = new HeaderMap
75 postDatas["Bugs Bunny"] = "Daffy Duck"
76 postDatas["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*"
77 postDatas["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only once, the last one"
78 request.datas = postDatas
79 request.verbose = false
80 var postResponse = request.execute
81
82 print "Our body from the callback : {myHttpFetcher.our_body}"
83
84 if postResponse isa CurlResponseSuccess then
85 print "*** Answer ***"
86 print "Status code : {postResponse.status_code}"
87 print "Body should be empty, because we decided to manage callbacks : {postResponse.body_str.length}"
88 else if postResponse isa CurlResponseFailed then
89 print "Error code : {postResponse.error_code}"
90 print "Error msg : {postResponse.error_msg}"
91 end
92
93 # HTTP Get to file Request
94 else if args[0] == "GET_FILE" then
95 var headers = new HeaderMap
96 headers["Accept"] = "Moo"
97 request.headers = headers
98 request.verbose = false
99 var downloadResponse = request.download_to_file(null)
100
101 if downloadResponse isa CurlFileResponseSuccess then
102 print "*** Answer ***"
103 print "Status code : {downloadResponse.status_code}"
104 print "Size downloaded : {downloadResponse.size_download}"
105 else if downloadResponse isa CurlResponseFailed then
106 print "Error code : {downloadResponse.error_code}"
107 print "Error msg : {downloadResponse.error_msg}"
108 end
109 # Program logic
110 else
111 print "Usage : Method[POST, GET, GET_FILE]"
112 end
113 end