twingly / twingly-search-api-java

:coffee: Twingly Blog Search API in Java
https://developer.twingly.com/resources/search/
3 stars 7 forks source link
betamax java

Twingly Search API Java GitHub Build Status

Java client for Twingly Search API (previously known as Twingly Analytics API). Twingly is a blog search service that provides a searchable API known as Twingly Search API.

Installation

Get the latest release from Maven Central.

Usage

import com.twingly.search.*;
public class Test{
    public static void main(String[] args){
        String apiKey = "API_KEY";
        // create Client object using your API key
        Client client = new UrlConnectionClient(apiKey);
        String q = "spotify";
        Result result = client.makeRequest(q);
        // Get the total number of matches returned
        int numberOfMatchesReturned = result.getNumberOfMatchesReturned();
        // Get the total number of matches to the search query
        int numberOfMatchesTotal = result.getNumberOfMatchesTotal();
        // Get number of seconds it took to execute query
        double secondsElapsed = result.getSecondsElapsed();
        for (Post p : result.getPosts()){
            System.out.println(p.getUrl());             // print url for each post
            System.out.println(p.getTitle());           // print title for each post
        }
    }
}

To learn more about the features of this client, check out the example code that can be found in example.

To learn more about the capabilities of the API, please read the Twingly Search API documentation.

API key from Java system properties

Additionally, you can create Client object with API key from Java system properties (property name is TWINGLY_SEARCH_KEY):

import com.twingly.search.client.*;
public class Test{
    public static void main(String[] args){
        Client client = new UrlConnectionClient(); // create client object with API key from java system property
    }
}

To do so, you will need to run Java with property. For example, run your own jar file with twingly search key property:

java -jar myJar.jar -DTWINGLY_SEARCH_KEY=some_key_value

Exception handling

Given exception hierarchy is now available:

Check Twingly Search API documentation for additional information about error codes.

Requirements

Development

Tests

Make sure you have all the dependencies

gradlew assemble

Run the tests

gradlew check

Generate coverage report, using JaCoCo. Note that you need to run the tests first.

gradlew jacocoTestReport

open build/jacocoHtml/index.html

Betamax

To be able to run the Betamax-based tests you need to add Betamax's certificate into Java's trust store, cacerts:

sudo keytool -importcert -keystore $JAVA_HOME/jre/lib/security/cacerts -file betamax.pem -alias betamax -storepass changeit -noprompt

It is adviced to remove the certificate after testing is done:

sudo keytool -delete -keystore $JAVA_HOME/jre/lib/security/cacerts -alias betamax -storepass changeit -noprompt

Note that the temporary files generated by Betamax:

need to be removed if generated before Betamax's certificate was added to the trust store, tests will fail otherwise.

For more information, check out Betamax's SSL configuration documentation.

JavaDoc generation

To generate java docs just run gradle task javadoc:

gradlew javadoc

This will save javadocs in docs folder under build-folder.

One-jar generation

To generate one fat jar library with all compile dependencies just run gradle task generateOneJar:

gradlew generateOneJar

This will save JAR file with all needed dependencies in build/libs/ with -all suffix. (like twingly-search-api-java-all-0.0.1.jar)

Release

Before you start

Make the release

  1. Ensure you are using Java 7
  2. Bump the version in version.properties
  3. Commit the changes
  4. Tag the current commit with the same version number and push it
  5. Run gradlew uploadArchives -Prelease to release to the staging repository
  6. Log into Nexus Repository Manager and find the archive
  7. Read Releasing the Deployment
  8. Release to Central Repository using Nexus Repository Manager

Update the changelog

License

The MIT License (MIT)

Copyright (c) 2012-2016 Twingly AB

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.