Giter Club home page Giter Club logo

Comments (8)

dtolnay avatar dtolnay commented on June 22, 2024 1

I edited a checklist into the OP and filed individual issues to follow up on the remaining ones.

from rust-cookbook.

AndyGauge avatar AndyGauge commented on June 22, 2024 1

The cookbook is a bit heavy on HTTP patterns. There is no additional comments here that haven't been completed. I'm closing this issue.

from rust-cookbook.

brson avatar brson commented on June 22, 2024

Yes, this is a great idea, and non-trivial. Maybe some of these ideas can be split into multiple examples to avoid overwhelming.

What crate's would we need for the most basic example? reqwest, serde_json, url, others?

from rust-cookbook.

konstin avatar konstin commented on June 22, 2024

For my crate I use reqwest and json. I can't use serde due to the freedom in the api specification. Url is included as transitive dependency and re-exported through reqwest. It might be reasonable to provide examples with json and with serde as each has its usecases.

The bullet points should have a good size for a single example. The following is e.g. my code for GET, parsing and returning json. (I'm very open to improvements - it's the best I was able to write using the current docs)

fn get_json(&self, url: Url) -> Result<JsonValue, Box<Error>> {
    println!("Loading: {:?}", &url);
    let mut reponse = reqwest::get(url)?;
    if !reponse.status().is_success() {
        return Err(From::from(format!("Bad status code returned for request: {}",
                                      reponse.status())));
    }

    let mut json_string = String::new();
    reponse.read_to_string(&mut json_string)?;
    Ok(json::parse(&json_string)?)
}

Another idea that comes to my mind is including an example using chrono for proper timestamp formatting.

from rust-cookbook.

dtolnay avatar dtolnay commented on June 22, 2024

Would you be able to elaborate on this part?

I can't use serde due to the freedom in the api specification.

This is what I would expect for analogous code using Serde:

extern crate serde_json;

use serde_json::Value;

fn get_json(&self, url: Url) -> Result<Value, Box<Error>> {
    println!("Loading: {:?}", &url);
    let response = reqwest::get(url)?;
    if !response.status().is_success() {
        return Err(From::from(format!("Bad status code returned for request: {}",
                                      response.status())));
    }
    Ok(serde_json::from_reader(response)?)
}

from rust-cookbook.

konstin avatar konstin commented on June 22, 2024

I though serde was for deserializing into structs. But with that new option I'm definitely going to try serde! So there's likely no need for json-rust at all

from rust-cookbook.

brson avatar brson commented on June 22, 2024

@konstin a self-contained example about the size of the one in your previous post would be a fine addition.

from rust-cookbook.

budziq avatar budziq commented on June 22, 2024

Hi, I have implemented two relevant examples ("Query the GitHub API" idea taken from #24)

HTTP GET repo stargazers:
https://play.integer32.com/?gist=37adc825a9f373a3d2d38f0ab7a8672f&version=undefined

HTTP POST and DELETE: create a gist file and delete it
https://play.integer32.com/?gist=119d16cef32681c96a04058ba6c3514c&version=undefined

These still need cleanup and textual description etc.. Hopefully I will have some time tomorrow.

from rust-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.