com.mtnfog.metatext.MetaTextStandardClient.java Source code

Java tutorial

Introduction

Here is the source code for com.mtnfog.metatext.MetaTextStandardClient.java

Source

/* Copyright 2016 Mountain Fog, Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.mtnfog.metatext;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.gson.Gson;
import com.mtnfog.metatext.service.MetaTextService;
import com.squareup.okhttp.OkHttpClient;

import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;

/**
 * The MetaTextClient supports sending requests to
 * the MetaText service and receiving responses.
 * 
 * @author Mountain Fog, Inc.
 */
public class MetaTextStandardClient implements MetaTextClient {

    public static final String MTNFOG_METATEXT_ENDPOINT = "https://api.mtnfog.com/metatext/";

    private String apiKey;
    private MetaTextService service;

    /**
     * Initialize a new client with your API key.
     * @param apiKey Your MetaText API key.
     */
    public MetaTextStandardClient(String apiKey, String endpoint) {

        this.apiKey = apiKey;

        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(120, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
                .setConverter(new GsonConverter(new Gson())).setClient(new OkClient(okHttpClient)).build();

        service = restAdapter.create(MetaTextService.class);

    }

    @Override
    public String disambiguate(String entity, String text, String entityClass) {

        String entityURI = service.disambiguate(entity, text, entityClass, apiKey);

        return entityURI;

    }

    @Override
    public Map<String, String> enrich(String entityUri, int maxEnrichments) {

        Map<String, String> enrichments = service.enrich(entityUri, maxEnrichments, apiKey);

        return enrichments;

    }

    /**
     * Get the MetaText API key.
     * @return The MetaText API key.
     */
    public String getApiKey() {
        return apiKey;
    }

    /**
     * Set the MetaText API key.
     * @param apiKey The MetaText API key to use.
     */
    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

}