api::browse now returns json response

This commit is contained in:
Antoine Gersant 2016-08-18 23:29:27 -07:00
parent 63d10fa695
commit 474710a6a9
5 changed files with 37 additions and 13 deletions

1
Cargo.lock generated
View file

@ -5,6 +5,7 @@ dependencies = [
"iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"router 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"staticfile 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.2.0 (git+https://github.com/servo/rust-url)",
]

View file

@ -9,6 +9,9 @@ version = "0.4.0"
[dependencies.url]
git = "https://github.com/servo/rust-url"
[dependencies]
rustc-serialize = "0.3"
[dependencies]
router = "*"
staticfile = "*"

View file

@ -5,10 +5,19 @@ use std::ops::Deref;
use iron::prelude::*;
use iron::status;
use mount::Mount;
use rustc_serialize::json;
use url::percent_encoding::percent_decode;
use collection;
impl From<collection::CollectionError> for IronError {
fn from(err: collection::CollectionError) -> IronError {
match err {
collection::CollectionError::Io(e) => IronError::new(e, status::NotFound)
}
}
}
pub fn get_api_handler() -> Mount {
let mut mount = Mount::new();
mount.mount("/browse/", self::browse)
@ -25,11 +34,19 @@ fn path_from_request(request: &Request) -> Result<PathBuf, Utf8Error> {
fn browse(request: &mut Request) -> IronResult<Response> {
let path = path_from_request(request);
if path.is_err() {
return Ok(Response::with((status::BadRequest)));
return Ok(Response::with(status::BadRequest));
}
let browse_result = collection::browse(&path.unwrap());
println!("{:?}", browse_result.unwrap_or(vec![])); // TMP
Ok(Response::with((status::Ok, "TODO browse data here")))
let path = path.unwrap();
let browse_result = try!(collection::browse(&path));
let result_json = json::encode(&browse_result);
if result_json.is_err() {
return Ok(Response::with(status::InternalServerError));
}
let result_json = result_json.unwrap();
println!("{:?}", browse_result); // TMP
Ok(Response::with((status::Ok, result_json)))
}
fn flatten(request: &mut Request) -> IronResult<Response> {

View file

@ -3,14 +3,16 @@ use std::io;
use std::path::Path;
use std::path::PathBuf;
#[derive(Debug)]
pub struct Song {
path: PathBuf,
}
#[derive(Debug)]
#[derive(Debug, RustcEncodable)]
pub struct Song(PathBuf);
#[derive(Debug, RustcEncodable)]
pub struct Directory(PathBuf);
#[derive(Debug, RustcEncodable)]
pub enum CollectionFile {
Directory(PathBuf),
Directory(Directory),
Song(Song),
}
@ -36,10 +38,10 @@ pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
let file_meta = try!(file.metadata());
let file_path = file.path().to_owned();
if file_meta.is_file() {
let collection_file = CollectionFile::Song(Song {path: file_path });
let collection_file = CollectionFile::Song(Song(file_path));
out.push(collection_file);
} else if file_meta.is_dir() {
let collection_file = CollectionFile::Directory(file_path);
let collection_file = CollectionFile::Directory(Directory(file_path));
out.push(collection_file);
}
}

View file

@ -1,6 +1,7 @@
extern crate core;
extern crate iron;
extern crate mount;
extern crate rustc_serialize;
extern crate staticfile;
extern crate url;