GithubHelp home page GithubHelp logo

richarddes / newsapi-golang Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 47 KB

A simple to use Golang client for the NewsAPI service

License: MIT License

Go 100.00%
newsapi golang-package newsapi-golang golang-news golang-news-client golang golang-libary

newsapi-golang's Introduction

NewsAPI Golang

GoDoc

A simple to use Golang client for the NewsAPI service.

Before you use the package you should read the NewsAPI docs to get familiar with the endpoints and their options.

Install

go get github.com/richarddes/newsapi-golang

Usage

Every project has to first initialize a client obejct with an API key like so:

c := newsapi.Client{APIKey: "your-api-key"}

After that, one of three methods of the Client object can be called. The methods are called TopHeadlines, Everything and Sources. Each one of accepts a context and an options struct which is called exactly like the method plus the word "Opts" at the end. Here's an example of fetching the top headlines from the UK:

ctx := context.Background()

opts := newsapi.TopHeadlinesOpts{
  Country: "uk",
}

r, err := c.TopHeadlines(ctx, opts)
if err != nil {
	log.Fatal(err)
}

When fetching top headlines at least one of the following options must be specified: Q, Category, Country or Sources You also cannot specify the Sources option in conjunction with the Category or Country option.

When fetching everything at least one of the following options must be specified: Q, QInTitle, Sources or Domains

For more details about the options structs please refer to the docs.

Since the TopHeadlines and Everything routes both return a response type of the same underlying type called "articleResp", you can cast them from one to another:

thr := newsapi.TopHeadlinesResp{}
er := newsapi.EverythingResp(t)

The decision to give the two routes different response types has been made to make the API more explicit.

The API also has an "Article" type which represents an article in the "Articles" field of the "TopHeadlinesResp" or "EverythingResp" object. It's useful if, for instance, you want to store the Articles returned by the TopHeadlines and Everything routes in a database. A full example of how to do this can be found down below.
Here's a quick example of how to print the Titles of all articles returned by the TopHeadlines route:

ctx := context.Background()

opts := newsapi.TopHeadlinesOpts{
  Country: "uk",
}

r, err := c.TopHeadlines(ctx, opts)
for _, a in range r.Articles {
	fmt.Println(article.Title)	
}

Full Example

Here's a full runnable example on how to fetch the top headlines in the "business" category and save the recieved articles in a PostgreSQL database.The articles are being saved in a table with following schema:
news(url TEXT PRIMARY KEY, author TEXT, title TEXT, source TEXT)

package main

import (
	"context"
	"database/sql"
	"log"

	"github.com/richarddes/newsapi-golang"
	_ "github.com/lib/pq"
)

func main() {
	db, err := sql.Open("postgres", "user=user password=password host=localhost port=5432")
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	stmt, err := db.Prepare("INSERT INTO news VALUES($1,$2,$3,$4);")
	if err != nil {
		log.Fatal(err)
	}

	defer stmt.Close()

	ctx := context.Background()

	c := newsapi.Client{APIKey: "your-api-key"}
	opts := newsapi.TopHeadlinesOpts{
		Country: "uk",
	}

	r, err := c.TopHeadlines(ctx, opts)
	if err != nil {
		log.Fatal(err)
	}

	for _, article := range r.Articles {
		_, err := stmt.Exec(article.URL, article.Author, article.Title, article.Source)
		if err != nil {
			log.Fatal(err)
		}
	}
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT License. Click here or see the LICENSE file for details.

newsapi-golang's People

Watchers

 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.