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