Example use of the Curl module

Introduced classes

class MyHttpFetcher

curl :: MyHttpFetcher

Custom delegate to receive callbacks from a Curl transfer

Redefined classes

redef class Sys

curl :: curl_http $ Sys

The main class of the program.

All class definitions

class MyHttpFetcher

curl $ MyHttpFetcher

Custom delegate to receive callbacks from a Curl transfer
redef class Sys

curl :: curl_http $ Sys

The main class of the program.
package_diagram curl::curl_http curl_http curl curl curl::curl_http->curl core core curl->core json json curl->json ...core ... ...core->core ...json ... ...json->json a_star-m a_star-m a_star-m->curl::curl_http

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module native_curl

curl :: native_curl

Binding of C libCurl which allow us to interact with network.
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module curl

curl :: curl

Data transfer powered by the native curl library

Children

module a_star-m

a_star-m

# Example use of the Curl module
module curl_http is example

import curl

# Custom delegate to receive callbacks from a Curl transfer
class MyHttpFetcher
	super CurlCallbacks

	# Body of the downloaded file
	var fetched_body = ""

	redef fun header_callback(line) do
		# We keep this callback silent for testing purposes
		#if not line.has_prefix("Date:") then print "Header_callback: {line}"
	end

	redef fun body_callback(line) do self.fetched_body += line

	redef fun stream_callback(buffer) do print "Stream_callback: {buffer}"
end

private fun print_usage do print "Usage: curl_http [POST|GET|GET_FILE] url"

if args.length < 2 then
	print_usage
	exit 1
end

var url = args[1]
var request = new CurlHTTPRequest(url)
request.verbose = false # Set to `true` to debug

if args[0] == "GET" then
	# HTTP Get Request
	var response = request.execute

	if response isa CurlResponseSuccess then
		print "Status code: {response.status_code}"
		print "Body: {response.body_str}"
	else if response isa CurlResponseFailed then
		print "Error code: {response.error_code}"
		print "Error msg: {response.error_msg}"
	end

else if args[0] == "POST" then
	# HTTP Post Request
	var my_http_fetcher = new MyHttpFetcher
	request.delegate = my_http_fetcher

	var post_data = new HeaderMap
	post_data["Bugs Bunny"] = "Daffy Duck"
	post_data["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*"
	post_data["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only one, the last one"
	request.data = post_data
	var response = request.execute

	print "Our body from the callback: {my_http_fetcher.fetched_body}"

	if response isa CurlResponseSuccess then
		print "*** Answer ***"
		print "Status code: {response.status_code}"
		print "Body should be empty, because we decided to manage callbacks: {response.body_str.length}"
	else if response isa CurlResponseFailed then
		print "Error code: {response.error_code}"
		print "Error msg: {response.error_msg}"
	end

else if args[0] == "GET_FILE" then
	# HTTP Get to file Request
	var headers = new HeaderMap
	headers["Accept"] = "Moo"
	request.headers = headers
	var response = request.download_to_file(null)

	if response isa CurlFileResponseSuccess then
		print "*** Answer ***"
		print "Status code: {response.status_code}"
		print "Size downloaded: {response.size_download}"
	else if response isa CurlResponseFailed then
		print "Error code: {response.error_code}"
		print "Error msg: {response.error_msg}"
	end

else
	print_usage
	exit 1
end
lib/curl/examples/curl_http.nit:17,1--104,3