lib: fixing tests programs of curl
[nit.git] / 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 do self.curl = new Curl
30
31 # Release curl object
32 fun destroy do self.curl.destroy
33
34 fun request(url: String): nullable CurlRequest do return self.curl.http_request(url)
35
36 # Header callback
37 redef fun header_callback(line: String) do
38 # We keep this callback silent for testing purposes
39 #if not line.has_prefix("Date:") then print "Header_callback : {line}"
40 end
41
42 # Body callback
43 redef fun body_callback(line: String) do self.our_body = "{self.our_body}{line}"
44
45 # Stream callback - Cf : No one is registered
46 redef fun stream_callback(buffer: String, size: Int, count: Int) do print "Stream_callback : {buffer} - {size} - {count}"
47 end
48
49
50 # Program
51 if args.length < 2 then
52 print "Usage {sys.program_name} <method wished [POST, GET, GET_FILE]> <target url>"
53 else
54
55 var myHttpFetcher = new MyHttpFetcher
56 var url = args[1]
57
58 # HTTP Get Request
59 if args[0] == "GET" then
60 var getContentRequest = myHttpFetcher.request(url)
61 if getContentRequest isa CurlHTTPRequest then
62
63 getContentRequest.verbose = false
64 var getResponse = getContentRequest.execute
65
66 if getResponse isa CurlResponseSuccess then
67 print "Status code : {getResponse.status_code}"
68 print "Body : {getResponse.body_str}"
69 else if getResponse isa CurlResponseFailed then
70 print "Error code : {getResponse.error_code}"
71 print "Error msg : {getResponse.error_msg}"
72 end
73
74 else
75 print "Wrong init with Curl HTTP request"
76 end
77
78 # HTTP Post Request
79 else if args[0] == "POST" then
80 var postContentRequest = myHttpFetcher.request(url)
81 if postContentRequest isa CurlHTTPRequest then
82
83 postContentRequest.delegate = myHttpFetcher
84 var postDatas = new HeaderMap
85 postDatas["Bugs Bunny"] = "Daffy Duck"
86 postDatas["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*"
87 postDatas["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only once, the last one"
88 postContentRequest.datas = postDatas
89 postContentRequest.verbose = false
90 var postResponse = postContentRequest.execute
91
92 print "Our body from the callback : {myHttpFetcher.our_body}"
93
94 if postResponse isa CurlResponseSuccess then
95 print "*** Answer ***"
96 print "Status code : {postResponse.status_code}"
97 print "Body should be empty, because we decided to manage callbacks : {postResponse.body_str.length}"
98 else if postResponse isa CurlResponseFailed then
99 print "Error code : {postResponse.error_code}"
100 print "Error msg : {postResponse.error_msg}"
101 end
102
103 else
104 print "Wrong init with Curl HTTP request"
105 end
106
107 # HTTP Get to file Request
108 else if args[0] == "GET_FILE" then
109 var downloadFileRequest = myHttpFetcher.request(url)
110 if downloadFileRequest isa CurlHTTPRequest then
111
112 var headers = new HeaderMap
113 headers["Accept"] = "Moo"
114 downloadFileRequest.headers = headers
115 downloadFileRequest.verbose = false
116 var downloadResponse = downloadFileRequest.download_to_file(null)
117
118 if downloadResponse isa CurlFileResponseSuccess then
119 print "*** Answer ***"
120 print "Status code : {downloadResponse.status_code}"
121 print "Size downloaded : {downloadResponse.size_download}"
122 else if downloadResponse isa CurlResponseFailed then
123 print "Error code : {downloadResponse.error_code}"
124 print "Error msg : {downloadResponse.error_msg}"
125 end
126
127 else
128 print "Wrong init with Curl HTTP request"
129 end
130
131 # Program logic
132 else
133 print "Usage : Method[POST, GET, GET_FILE]"
134 end
135 end