Home > Software design >  Sorting news api endpoint
Sorting news api endpoint

Time:11-03

My task is to sort everything and top headlines articles at the same click.

I'm using this news api: https://newsapi.org/

My endpoints at the moment:

@GET("top-headlines?sources=bbc-news&apiKey=${BuildConfig.API_KEY}")
suspend fun getTopHeadlinesArticles(
    @Query("page")  page:Int = 1
) : Response<ArticleListResponse>

@GET("everything?q=tesla&sortBy=publishedAt&apiKey=${BuildConfig.API_KEY}")
suspend fun getAllArticles(
    @Query("page") page: Int = 1
) : Response<ArticleListResponse>

Problem with sortBy is that it only is available in everything endpoint if I use it in top headlines it does nothing. I tried with sortBy=publishedAt and -publishedAt.

Json example:

"status": "ok",
"totalResults": 37,
"articles": [

    {
        "source": {
            "id": "reuters",
            "name": "Reuters"
        },
        "author": null,
        "title": "Russia says it resumes participation in Ukraine grain deal - Reuters.com",
        "description": "Russia said on Wednesday it would resume its participation in a deal to free up vital grain exports from war-torn Ukraine after suspending it over the weekend in a move that had threatened to exacerbate hunger across the world.",
        "url": "https://www.reuters.com/world/europe/grain-ships-sail-ukraine-ports-russian-missiles-knock-out-power-across-country-2022-10-31/",
        "urlToImage": "https://www.reuters.com/resizer/0q0d5ZOWPFOkR2Yc_Mbv8TQZrAA=/1200x628/smart/filters:quality(80)/cloudfront-us-east-2.images.arcpublishing.com/reuters/NQYU2CZKHRNKRDPQ76IQYOV2DE.jpg",
        "publishedAt": "2022-11-02T10:43:00Z",
        "content": "ANKARA/MYKOLAIV, Ukraine, Nov 2 (Reuters) - Russia said on Wednesday it would resume its participation in a deal to free up vital grain exports from war-torn Ukraine after suspending it over the week… [ 2828 chars]"

From news api documentation: Articles are sorted by the earliest date published first.

I want to sort it ascending and descending.

CodePudding user response:

The top-headlines endpoint's documentation lists all the possible request parameters, and none of them control the sorting. So, unfortunately, it's not possible to change the sorting for this endpoint. You can try submitting a feature request to them by sending an email to [email protected].

  • Related