Giter Club home page Giter Club logo

newsapi.net's Introduction

Build status

ℹ Add NewsAPI.Net to your project.

From the PackageManager CLI

PM> Install-Package News.Net -Version 1.0.0

From the DotNet CLI

> dotnet add package News.Net --version 1.0.0

Or place this line in your .csproj & do a dotnet restore.

<PackageReference Include="News.Net" Version="1.0.0" />

🤔 How To Use

Creating a request to send to the API.

//Generate a new AllNewsRequest. This is where you create your query for the API.
var request = new AllNewsRequest("Arrow", SortType.PublishedDate);

//You can also set date from and to if you so wish.
//This will fetch all news regarding Arrow that have been published in the last 7 days.
var request = new AllNewsRequest("Arrow", SortType.PublishedDate, DateTime.UtcNow.AddDays(-7));

//This will fetch all news regarding Arrow 
//This request exmample states that articles are to be:
//No older than 7 days and also ignore articles from the last 2 days.
//For example if it where 10/1/2019, this would be news articles from The 3/1/2019 to 8/1/2019 
var request = new AllNewsRequest("Arrow", SortType.PublishedDate, DateTime.UtcNow.AddDays(-7), DateTime.UtcNow.AddDays(-2));

Using those requests.

//Initialise a NewClient & pass in your APIKey
//Get the NewsClient from your IOC Container if you are using one.
var newsClient = new NewsClient("API_Key Here");

//Send the request you created (as shown above)
var result = await newsClient.FetchNewsAsync(request);

//You will now have a NewsResult, of which you will want to check the status.
//If the status has not shown OK, you handle it.
if (result.ResponseStatus != ResponseStatus.Ok)
{
    Console.WriteLine($"There seems to have been an error. {result.Error.ToString()}")
}

//Else you can use the result.Articles to access the article information.
else
{
    foreach (var article in result.Articles)
    {
        Console.WriteLine(article.Title);
        Console.WriteLine(article.Description);
        Console.WriteLine(article.Author);
        Console.WriteLine(article.Source.Name);
    }
}

Getting top headline results for a desired source.

//as above, create your client or get it from your IOC Container.
//Use the NewsClient to send a request for the latest news from a desired source.
var result = await _newsClient.FetchNewsFromSource(NewsSource.BBC);

//You can also pass in an array of sources if you wish.
var sources = new NewsSource[] 
    { 
        NewsSource.ABCNews,
        NewsSource.BBC
    };

var result = await _newsClient.FetchNewsFromSource(sources);

While I do support a large amount of sources by default that are provided by the NewsApi.Org website. I DO NOT support them all. If you want to use a source I do not support, you are able to enter the source id like so. (Find the source ID's Here)

var result = await _newsClient.FetchNewsFromSource("buzzfeed");

The IServiceCollection extension method.

This is a simple way to add the service to your IOC Container.

public class Foo
{
    public void SetupServices()
    {
        serviceCollection.UseNewsAPI("YOUR_API_KEY_HERE");
    }
}

newsapi.net's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mjb52

newsapi.net's Issues

Implement Top Headlines endpoint.

This endpoint is documented Here

All that is needed is to generate a query for top headlines in much the same way as we handle the everything endpoint currently.

ℹ️ It might actually make sense to refactor the current NewsRequest entity due to some of the properties not being needed or used at all in a query on the TopHeadlines api endpoint.

Implement a method for generating request queries.

Currently the Request Query is generated by simply newing up a NewsRequest. The limitation of this is that the NewsRequest has the wrong properties for a Top Headlines api request.

To fix that I am thinking there will be a method on the NewsClient that is named GenerateRequest. It would take a RequestType and return an instance of the correct NewsRequest model.

This would mean a rather big refactor of the current way we generate a request but most of it would be naming. Example end user implementation:

var request = newsClient.GenerateRequest(RequestType.TopHeadlines);
request.AddSource("Some Source Such as BBC");
request.SetCountry(Countries.Uk);
request.Filter("Some Filter Query such as .NetCore");

var result = newsClient.FetchNews(request);

Ambiguity between "NewsResponse" and "NewsResult"

These two classes contain seemingly the same thing, except the NewsResponse also has a Message property. Do you generate a NewsResult just to hide the Message property from the user? If so, is that really necessary?

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.