newsapi

package module
v0.0.0-...-00c6a4f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2018 License: MIT Imports: 7 Imported by: 1

README

News Api Golang

Client for the News API

Installation

You can simply do go get github.com/kaelanb/newsapi-go

DOC

Full Documentation for all the endpoints can be found here

Request parameters:

  • country : ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za

  • category: business entertainment general health science sports technology (can't be mixed with sources parameter)

  • sources: news sources

  • q: keywords to search for

  • pageSize: page size

  • page: page number

Example

import (
	"fmt"
	"log"

	newsapi	"github.com/kaelanb/newsapi-go"
)

func main() {
	apikey := "apikeyhere"
	client := newsapi.New(apikey)

	query1 := []string{"country=ca"}
	newsResponse, err := client.GetTopHeadlines(query1)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(newsResponse)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Article

type Article struct {
	Source      ArticleSource `json:"source"`
	Author      string        `json:"author"`
	Title       string        `json:"title"`
	Description string        `json:"description"`
	URL         string        `json:"url"`
	URLToImage  string        `json:"urlToImage"`
	PublishedAt time.Time     `json:"publishedAt"`
}

Article struct for unmarshaling article json data.

type ArticleSource

type ArticleSource struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

ArticleSource struct for unmarshaling source data embeded in article json.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client for api requests.

func New

func New(apiKey string) *Client

New returns initialized client.

func (*Client) GetEverything

func (cli *Client) GetEverything(args []string) (*NewsResponse, error)

GetEverything Get every article relevant to given parameters.

func (*Client) GetSources

func (cli *Client) GetSources(args []string) (*SourcesResponse, error)

GetSources get all sources relevant to given parameters.

func (*Client) GetTopHeadlines

func (cli *Client) GetTopHeadlines(args []string) (*NewsResponse, error)

GetTopHeadlines get top news headlines relevant to given parameters.

type Error

type Error struct {
	Status  string `json:"status"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

Error json struct for error message.

func (Error) Error

func (err Error) Error() string

Error implements error interface.

type NewsResponse

type NewsResponse struct {
	Status       string    `json:"status"`
	TotalResults int       `json:"totalResults"`
	Articles     []Article `json:"articles"`
}

NewsResponse json struct for news articles.

type Source

type Source struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	URL         string `json:"url"`
	Category    string `json:"category"`
	Language    string `json:"language"`
	Country     string `json:"country"`
}

Source struct for unmarshaling source json data.

type SourcesResponse

type SourcesResponse struct {
	Status  string   `json:"status"`
	Sources []Source `json:"sources"`
}

SourcesResponse json struct for source.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL