EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
605 stars 201 forks source link

Example beer API seems to be not available anymore #322

Closed thomasgloe closed 3 months ago

thomasgloe commented 3 months ago

I've tried the example application, but it seems that the employed API: https://api.punkapi.com/v2/beers (remote_api.dart) has already gone.

Official statement from the website says that the API will be shutdown on 01/05/2024 ("PunkAPI is shutting down on May 1st 2024") @ https://punkapi.com/documentation/v2.

It seems that the source code for the API itself is not available. I've found only the DB employed in the API: https://github.com/sammdec/punkapi

thomasgloe commented 3 months ago

Here is a quick and dirty web server providing the employed API in the example (written in Rust). The api does only support the endpoint required by the example.

Open points:

Filename: main.rs

mod models {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Deserialize, Serialize, Clone)]
    pub struct BeerSummary {
        pub id: u64,
        pub name: String,
        pub image_url: String,
    }
}

mod handlers {
    use super::models::BeerSummary;
    use std::collections::HashMap;

    pub async fn get_beer(
        params: HashMap<String, String>,
    ) -> Result<impl warp::Reply, warp::Rejection> {
        // parameter: per_page
        let dval = "20".to_string();
        let tval = params.get("per_page").unwrap_or(&dval);
        let per_page = match tval.parse::<u64>() {
            Ok(ival) => ival,
            Err(_) => 20, // default value
        };
        // parameter: page
        let dval = "0".to_string();
        let tval = params.get("page").unwrap_or(&dval);
        let page = match tval.parse::<u64>() {
            Ok(ival) => ival,
            Err(_) => 0, // default value
        };
        // parameter: beer_name / TODO implement filter
        let beer_name = params.get("beer_name");

        let beers: Vec<BeerSummary> = ((page - 1) * per_page..page * per_page)
            .map(|id| BeerSummary {
                id,
                name: format!("Beer #{}", id),
                // TODO not all images are available, would be better to parse JSON files of punkapi-db
                image_url: format!("https://images.punkapi.com/v2/{}.png", id),
            })
            .collect();

        Ok(warp::reply::json(&beers))
    }
}

mod routes {
    use super::handlers;
    use std::collections::HashMap;
    use warp::Filter;

    pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
        get_beers()
    }

    fn get_beers() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
        warp::path!("v2" / "beers")
            .and(warp::get())
            .and(warp::query::<HashMap<String, String>>())
            .and_then(handlers::get_beer)
    }
}

#[tokio::main]
async fn main() {
    let routes = routes::routes();

    println!("Beer serving at http://localhost:8081");
    warp::serve(routes).run(([127, 0, 0, 1], 8081)).await;
}
clragon commented 3 months ago

Thank you for your issue. I have updated the example project on the develop branch. We will no longer be using the PunkAPI.