org.openmhealth.shim.fatsecret.FatsecretShim.java Source code

Java tutorial

Introduction

Here is the source code for org.openmhealth.shim.fatsecret.FatsecretShim.java

Source

/*
 * Copyright 2014 Open mHealth
 *
 * 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 org.openmhealth.shim.fatsecret;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;
import org.openmhealth.shim.*;

import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Encapsulates parameters specific to fatsecret api.
 *
 * @author Danilo Bonilla
 */
public class FatsecretShim extends OAuth1ShimBase {

    public static final String SHIM_KEY = "fatsecret";

    private static final String DATA_URL = "http://platform.fatsecret.com/rest/server.api";

    private static final String REQUEST_TOKEN_URL = "http://www.fatsecret.com/oauth/request_token";

    private static final String AUTHORIZE_URL = "http://www.fatsecret.com/oauth/authorize";

    private static final String TOKEN_URL = "http://www.fatsecret.com/oauth/access_token";

    private FatsecretConfig config;

    public FatsecretShim(AuthorizationRequestParametersRepo authorizationRequestParametersRepo,
            ShimServerConfig shimServerConfig, FatsecretConfig fatsecretConfig) {
        super(authorizationRequestParametersRepo, shimServerConfig);
        this.config = fatsecretConfig;
    }

    @Override
    public String getLabel() {
        return "Fat Secret";
    }

    @Override
    public List<String> getScopes() {
        return null; //noop!
    }

    @Override
    public String getShimKey() {
        return SHIM_KEY;
    }

    @Override
    public String getClientSecret() {
        return config.getClientSecret();
    }

    @Override
    public String getClientId() {
        return config.getClientId();
    }

    @Override
    public String getBaseRequestTokenUrl() {
        return REQUEST_TOKEN_URL;
    }

    @Override
    public String getBaseAuthorizeUrl() {
        return AUTHORIZE_URL;
    }

    @Override
    public String getBaseTokenUrl() {
        return TOKEN_URL;
    }

    @Override
    public ShimDataType[] getShimDataTypes() {
        return new ShimDataType[] {};
    }

    @Override
    public ShimDataResponse getData(ShimDataRequest shimDataRequest) throws ShimException {

        long numToSkip = 0;
        long numToReturn = 3;

        Calendar cal = Calendar.getInstance();
        cal.set(2014, Calendar.AUGUST, 1);
        Date endDate = new Date(cal.getTimeInMillis());
        cal.add(Calendar.DATE, -1);
        Date startDate = new Date(cal.getTimeInMillis());

        DateTime startTime = new DateTime(startDate.getTime());
        DateTime endTime = new DateTime(endDate.getTime());

        MutableDateTime epoch = new MutableDateTime();
        epoch.setDate(0);

        int days = 16283; //Days.daysBetween(epoch, new DateTime()).getDays() - 1;

        String endPoint = "food_entries.get";

        String accessToken = shimDataRequest.getAccessParameters().getAccessToken();
        String tokenSecret = shimDataRequest.getAccessParameters().getTokenSecret();

        URL url = signUrl(DATA_URL + "?date=" + days + "&format=json&method=" + endPoint, accessToken, tokenSecret,
                null);
        System.out.println("Signed URL is: \n\n" + url);

        // Fetch and decode the JSON data.
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonData;

        HttpGet get = new HttpGet(url.toString());
        HttpResponse response;
        try {
            response = httpClient.execute(get);
            HttpEntity responseEntity = response.getEntity();
            jsonData = objectMapper.readTree(responseEntity.getContent());
            return ShimDataResponse.result(FatsecretShim.SHIM_KEY, jsonData);

        } catch (IOException e) {
            throw new ShimException("Could not fetch data", e);
        } finally {
            get.releaseConnection();
        }
    }
}