1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Sample of the Curl module.
22 # Small class to represent an Http Fetcher
28 init do self.curl
= new Curl
31 fun destroy
do self.curl
.destroy
33 fun request
(url
: String): nullable CurlRequest do return self.curl
.http_request
(url
)
36 redef fun header_callback
(line
: String) do print
"Header_callback : {line}"
39 redef fun body_callback
(line
: String) do print
"Body_callback : {line}"
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}"
47 if args
.length
< 2 then
48 print
"Usage {sys.program_name} <method wished [POST, GET, GET_FILE]> <target url>"
51 var myHttpFetcher
= new MyHttpFetcher
55 if args
[0] == "GET" then
56 var getContentRequest
= myHttpFetcher
.request
(url
)
57 if getContentRequest
isa CurlHTTPRequest then
59 getContentRequest
.verbose
= false
60 var getResponse
= getContentRequest
.execute
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}"
71 print
"Wrong init with Curl HTTP request"
75 else if args
[0] == "POST" then
76 var postContentRequest
= myHttpFetcher
.request
(url
)
77 if postContentRequest
isa CurlHTTPRequest then
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
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}"
98 print "Wrong init with
Curl HTTP request
"
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
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)
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}"
122 print "Wrong init with
Curl HTTP request
"
127 print "Usage : Method[POST, GET, GET_FILE]"