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