com.jalepo.interfacedemo.ConnectionModule.java Source code

Java tutorial

Introduction

Here is the source code for com.jalepo.interfacedemo.ConnectionModule.java

Source

/*
 * Copyright 2016 Jason Powell
 *
 * 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.jalepo.interfacedemo;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.DELETE;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Dagger Module to inject the Retrofit services for HTTP connection
 */
@Module
public class ConnectionModule {

    /**
     * The base URL for the Retrofit instance
     */
    String mBaseUrl;

    // Constructor needs one parameter to instantiate.
    public ConnectionModule(String baseUrl) {
        this.mBaseUrl = baseUrl;
    }

    /**
     * Provides a GSON instance for the Retrofit converter.  Used internally by this module.
     * @return GSON object
     */
    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gsonBuilder.create();
    }

    /**
     * Provides the Retrofit instance for creating services.  Used internally by this module.
     * @param gson The GSON object for JSON parsing the responses.
     * @return The Retrofit object.
     */
    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson) {
        return new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(mBaseUrl)
                .build();
    }

    /**
     * Provides the UserProfileService to fetch the users profile
     * @param retrofit The Retrofit object
     * @return The UserProfileService
     */
    @Provides
    @Singleton
    UserProfileService provideUserProfileService(Retrofit retrofit) {
        return retrofit.create(UserProfileService.class);
    }

    /**
     * Provides the MediaCollectionFetchService to fetch the users recently posted/liked images.
     * @param retrofit The Retrofit object
     * @return The MediaCollectionFetchService
     */
    @Provides
    @Singleton
    MediaCollectionFetchService provideMediaCollectionFetchService(Retrofit retrofit) {
        return retrofit.create(MediaCollectionFetchService.class);
    }

    /**
     * Provides the FollowingFetchService to fetch the users follows.
     * @param retrofit The Retrofit object
     * @return The FollowingFetchService
     */
    @Provides
    @Singleton
    FollowingFetchService provideFollowingFetchService(Retrofit retrofit) {
        return retrofit.create(FollowingFetchService.class);
    }

    /**
     * Provides the MediaFetchService to fetch the data for a specific image.
     * @param retrofit The Retrofit object
     * @return The MediaFetchService
     */
    @Provides
    @Singleton
    MediaFetchService provideMediaFetchService(Retrofit retrofit) {
        return retrofit.create(MediaFetchService.class);
    }

    /**
     * Provides the MediaUnlikeService to unlike a specific image.
     * @param retrofit The Retrofit object
     * @return The MediaUnlikeService
     */
    @Provides
    @Singleton
    MediaUnlikeService provideMediaUnlikeService(Retrofit retrofit) {
        return retrofit.create(MediaUnlikeService.class);
    }

    /**
     * Provides the MediaLikeService to like a specific image.
     * @param retrofit The Retrofit object
     * @return The MediaLikeService
     */
    @Provides
    @Singleton
    MediaLikeService provideMediaLikeService(Retrofit retrofit) {
        return retrofit.create(MediaLikeService.class);
    }

    /**
     * GETs the users profile data
     */
    public interface UserProfileService {
        @GET("users/{user}")
        Call<ProfileModel> getUser(@Path("user") String user, @Query("access_token") String accessToken);

    }

    /**
     *  GETS the users recently liked/posted images.
     */
    public interface MediaCollectionFetchService {
        @GET("users/{user}/media/{status}")
        Call<MediaCollectionModel> getMedia(@Path("user") String user, @Path("status") String state,
                @Query("access_token") String accessToken);

    }

    /**
     * GETs the users that the current user is following
     */
    public interface FollowingFetchService {
        @GET("users/self/follows")
        Call<FollowingModel> getFollowing(@Query("access_token") String accessToken);

    }

    /**
     * GETs the metadata for a specific image
     */
    public interface MediaFetchService {
        @GET("media/{mediaId}")
        Call<MediaModel> getMedia(@Path("mediaId") String mediaId, @Query("access_token") String accessToken);

    }

    /**
     * DELETEs a like on a specific image.
     */
    public interface MediaUnlikeService {
        @DELETE("media/{mediaId}/likes")
        Call<ResponseBody> unlikeMedia(@Path("mediaId") String mediaId, @Query("access_token") String accessToken);

    }

    /**
     * POSTs a like on a specific image.
     */
    public interface MediaLikeService {
        //The endpoint to POST a like does not take the access token
        //as part of the URL..  The access token needs
        //to be form encoded in the body.
        @FormUrlEncoded
        @POST("media/{mediaId}/likes")
        Call<ResponseBody> likeMedia(@Path("mediaId") String mediaId, @Field("access_token") String accessToken);
    }
}