Coder Social home page Coder Social logo

newswrap's Introduction

Newswrap

CI crates.io

Hacker News API bindings for Rust.

use newswrap::{client::HackerNewsClient, errors::HackerNewsClientError};

#[tokio::main]
async fn main() -> Result<(), HackerNewsClientError> {
    // Build your client at the start of your application process
    let client = HackerNewsClient::new();

    // Call various endpoints with your client instance
    let generic_item = client.items.get_item(69).await?;
    dbg!(&generic_item);

    // Determine what the item type is
    let item_type = generic_item.get_item_type();
    dbg!(item_type);

    // Check if the item is job
    assert!(generic_item.is_story());

    // Retrieve user information
    let user = client.users.get_user("joeymckenzie").await?;
    dbg!(user);

    Ok(())
}

What is newswrap?

Newswrap provides a convenient Rust interface for the Hacker News API. Hacker News is a community-driven website targeted at software developers and technology professionals. While Hacker News offers up its API for the general public, there are no official language-based libraries for connecting to it. This project aims to provide an easy-to-use Rust-based client for retrieving data from Hacker News in an idiomatic fashion with first-class async support.

Using newswrap

To get started with newswrap within your Rust application, simply add the package,

cargo add newswrap

and spin up the client at the start of your application process:

use newswrap::{client::HackerNewsClient, errors::HackerNewsClientError};

#[tokio::main]
async fn main() -> Result<(), HackerNewsClientError> {
    // Build your client at the start of your application process
    let client = HackerNewsClient::new();

    // Check for the latest item ID
    let latest_id = client.realtime.get_latest_item_id().await?;

    // Get the latest stories IDs
    let story = client.items.get_story(69).await?;
    println!("{}... nice.", story.title);

    // Retrieve user profiles and information
    let my_profile = client.users.get_user("joeymckenzie").await?;

    if let Some(about_section) = my_profile.about {
        println!("{}", about_section);
    }

    Ok(())
}

Under the hood, newswrap relies on reqwest for collecting information from the Hacker News API via HTTP. It's advised for consumers of the newswrap client to instantiate a single instance at the start of your application process. Examples are available for using clients in binary applications and web applications (with axum).

newswrap's People

Contributors

joeymckenzie avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.