<commit_before>extern crate hyper;
extern crate rustc_serialize;

use std::io::Read;
use std::result::{Result};
use rustc_serialize::json::Json;
use hyper::{Client};

struct TheMovieDB {
	api_key: &'static str,
	base_url: &'static str,
	http_client: Client,
}

impl TheMovieDB {
	fn new(themoviedb_api_key: &'static str) -> Result<TheMovieDB, String> {
		let new_instance = TheMovieDB{api_key: themoviedb_api_key,
			base_url: "https://api.themoviedb.org/3",
			http_client: Client::new()};

		let configuration_data = new_instance.get_json_data_for("/configuration");
		println!("{:?}", configuration_data);

		Ok(new_instance)
	}

	fn get_url_for(& self, url_string: &'static str) -> String {
		if url_string.contains("?") {
			format!("{}{}&api_key={}", self.base_url, url_string, self.api_key)
		} else {
			format!("{}{}?api_key={}", self.base_url, url_string, self.api_key)
		}
	}

	fn get_json_data_for(& self, url_string: &'static str) -> Result<Json, String> {
		let call_url = self.get_url_for(url_string);

		let mut response = match self.http_client.get(&call_url).send() {
			Ok(r) => r,
			Err(_) => {
				return Err("Error during HTTPS request".to_string());
			}
 		};

		if hyper::Ok != response.status {
			return Err("No HTTPS Status 200 (OK)".to_string());
		}

		let mut result = String::new();
		if response.read_to_string(& mut result).is_err() {
			return Err("Error reading response".to_string());
		}

		let json_data = match Json::from_str(&result) {
			Ok(d) => d,
			Err(_) => return Err("Error while readin JSON data".to_string())
		};

		Ok(json_data)
	}

	fn test(& self) -> String {
		let call_url = format!("https://api.themoviedb.org/3/movie/5wcerweuioybrcqpwyr850?api_key={}", self.api_key);

		let mut res = self.http_client.get(&call_url).send().unwrap();
		assert_eq!(res.status, hyper::Ok);

		let mut result = String::new();
		res.read_to_string(& mut result).unwrap();

		let json_data = Json::from_str(&result).unwrap();
		let json_obj = json_data .as_object().unwrap();

	    json_obj.get("original_title").unwrap().as_string().unwrap().to_string()
	}
}

#[test]
fn it_works() {
	// TODO: Real tests ;)
	assert!("Four Rooms" == TheMovieDB { api_key: "6da14fa0b6231874a56ee667a505cdcc" }.test());
}

fn main() {
    let themdb = TheMovieDB::new("6da14fa0b6231874a56ee667a505cdcc");

    println!("{}", themdb.unwrap().test());
}
<commit_msg>Added fat::Debug and correct reading of secure image base URL<commit_after>extern crate hyper;
extern crate rustc_serialize;

use std::fmt;
use std::io::Read;
use std::result::{Result};
use rustc_serialize::json::Json;
use hyper::{Client};

struct TheMovieDB {
	api_key: &'static str,
	base_url: &'static str,
	image_base_url: String,
	http_client: Client,
}

impl fmt::Debug for TheMovieDB {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TheMovieDB => ApiKey: {}, BaseURL: {}, ImageBaseURL: {}",
		 self.api_key,
		 self.base_url,
		 self.image_base_url)
    }
}

impl TheMovieDB {
	/// Constructs a new TheMovieDB`.
	///
	/// # Examples
	///
	/// ```
	/// use std::rc::Rc;
	///
	/// let five = Rc::new(5);
	/// ```
	fn new(themoviedb_api_key: &'static str) -> Result<TheMovieDB, String> {
		let mut new_instance = TheMovieDB{api_key: themoviedb_api_key,
			base_url: "https://api.themoviedb.org/3",
			image_base_url: "".to_string(),
			http_client: Client::new()};

		let configuration_data = match new_instance.get_json_data_for("/configuration") {
			Ok(c) => c,
			Err(error_string) => return Err(error_string)
		};

		let json_obj = match configuration_data.as_object() {
			Some(o) => o,
			None => return Err("Error while getting JSON object".to_string())
		};

		new_instance.image_base_url = json_obj.get("images").unwrap().as_object().unwrap().get("secure_base_url").unwrap().to_string();

		Ok(new_instance)
	}

	fn get_url_for(& self, url_string: &'static str) -> String {
		if url_string.contains("?") {
			format!("{}{}&api_key={}", self.base_url, url_string, self.api_key)
		} else {
			format!("{}{}?api_key={}", self.base_url, url_string, self.api_key)
		}
	}

	fn get_json_data_for(& self, url_string: &'static str) -> Result<Json, String> {
		let call_url = self.get_url_for(url_string);

		let mut response = match self.http_client.get(&call_url).send() {
			Ok(r) => r,
			Err(_) => {
				return Err("Error during HTTPS request".to_string());
			}
 		};

		if hyper::Ok != response.status {
			return Err("No HTTPS Status 200 (OK)".to_string());
		}

		let mut result = String::new();
		if response.read_to_string(& mut result).is_err() {
			return Err("Error reading response".to_string());
		}

		let json_data = match Json::from_str(&result) {
			Ok(d) => d,
			Err(_) => return Err("Error while readin JSON data".to_string())
		};

		Ok(json_data)
	}
}

// #[test]
// fn it_works() {
// 	// TODO: Real tests ;)
// 	assert!("Four Rooms" == TheMovieDB { api_key: "6da14fa0b6231874a56ee667a505cdcc" }.test());
// }

fn main() {
    let themdb = TheMovieDB::new("6da14fa0b6231874a56ee667a505cdcc").unwrap();

	println!("{:?}", themdb);
}

{'fraction_non_alphanumeric': 0.17923970848926532, 'fraction_numerical': 0.017727004136300965, 'mean_word_length': 4.334033613445378, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1716170', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1890, 'n_words': 376}