NewsAPIClient 0.2.6

NewsAPIClient 0.2.6

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Andrea Marcolin.



NewsAPIClient

Requirements

iOS 8.0+ Swift 3.0+

Installation

NewsAPIClient is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "NewsAPIClient"

Usage

First, create a client with your NewsAPI.org API Key

let client = NewsAPIClient(apiKey: "<YOUR_API_KEY>")

The client provides two methods to interact with the two API endpoints: “/sources” and “/articles”. N.B. all operations are performed on the main thread. It is highly reccomended to dispatch each request to a background queue, and pass a completion handler which eventually performs UI updates on the main thread, accordingly with your needs. A general example can be found at http://stackoverflow.com/questions/24056205/how-to-use-background-thread-in-swift

Sources

Send a request to the “/sources” endpoint to get all sources

client.getSources { (sources, error) in
    guard let sources = sources else {
        print(error!)
        return
    }
    print(sources)
}

Optionally filter the sources by category, language or country

client.getSources(category: "business",
                  language: "en",
                  country: "gb")
{ (sources, error) in

    guard let sources = sources else {
        print(error!)
        return
    }

    print(sources)
}

Articles

Normally, chain the sources request with a request to the “/sources” endpoint to get articles from a specific source (sortBy is optional)

client.getSources { (sources, error) in
    guard let sources = sources else {
        print(error!)
        return
    }

    client.getArticles(sourceId: sources[0].id,
                       sortBy: sources[0].availableSortBys["latest"]) // if "latest" is not available for this source, defaults to "top" 
    { (articles, error) in

        guard let articles = articles else {
            print(error!)
            return
        }

        print(articles)
    }
}

Otherwise, if you already have a source object (i.e. previously obtained by the source method) just pass it to the getArticles() method to obtain articles for the given source Remember, sortby is optional, so in this case we provide an example without passing a sortBy.

client.getArticles(source: source) { (articles, error) in
    guard let articles = articles else {
        print(error!)
        return
    }

    print(articles)
}

Author

Andrea Marcolin, [email protected]

License

NewsAPIClient is available under the MIT license. See the LICENSE file for more info.