Module nytimes.mostpopular
ballerinax/nytimes.mostpopular Ballerina library
Overview
This is a generated connector from New York Times Most Popular API v2.0.0 OpenAPI specification. With the New York Times Most Popular API you can get popular articles on NYTimes.com. The Most Popular API provides services for getting the most popular articles on NYTimes.com based on emails, shares, or views.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create NYTimes developer account
- Obtain tokens
- Log into NYTimes Developer Portal by visiting https://developer.nytimes.com/accounts/login
- Register an app and obtain the API Key following the process summarized here.
Quickstart
To use the New York Times Most Popular connector in your Ballerina application, update the .bal file as follows:
Step 1: Import connector
First, import the ballerinax/nytimes.mostpopular
module into the Ballerina project.
import ballerinax/nytimes.mostpopular as nm;
Step 2: Create a new connector instance
Create a mostpopular:ApiKeysConfig
with the API key obtained, and initialize the connector with it.
nm:ApiKeysConfig config = { apiKey: "<API_KEY>" } nm:Client baseClient = check new Client(config);
Step 3: Invoke connector operation
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations.
Following is an example on how to get most emailed articles using the connector. You can now get most emailed articles on a certain section for a certain period. Here we have considered the "world" section for last 7 days.
Get most emailed articles
public function main() { nm:InlineResponse200|error response = baseClient->getMostemailed("world", "7"); if (response is nm:InlineResponse200) { nm:ArticleWithCountType[]? articlesWithCountType = response?.results; if !(articlesWithCountType is ()) { foreach var articleWithCountType in articlesWithCountType { log:printInfo(articleWithCountType?.'abstract.toString()); } } } else { log:printError(response.message()); } }
Following is an example on how to get most shared articles using the connector. You can now get most shared articles on a certain section for a certain period. Here we have considered the "sports" section for last 7 days.
Get most shared articles
public function main() { nm:InlineResponse2001|error response = baseClient->getMostshared("sports", "7"); if (response is nm:InlineResponse2001) { nm:Article[]? articles = response?.results; if !(articles is ()) { foreach var article in articles { log:printInfo(article?.'abstract.toString()); } } } else { log:printError(response.message()); } }
Following is an example on how to get most viewed articles using the connector. You can now get most shared articles on a certain section for a certain period. Here we have considered the "world" section for last 30 days.
Get most viewed articles
public function main() { nm:InlineResponse2001|error response = baseClient->getMostviewed("world", "30"); if (response is nm:InlineResponse2001) { nm:Article[]? articles = response?.results; if !(articles is ()) { foreach var article in articles { log:printInfo(article?.'abstract.toString()); } } } else { log:printError(response.message()); } }
-
Use
bal run
command to compile and run the Ballerina program.