Retrofit rest client with OAuth 1.0a in Magento for Android

MagentoRestApiWithRetrofit

As I have discussed in our previous article about  Magento REST API  with OAuth 1.0a authentication, in this article I am going to continue after that with RETROFIT. We have used Retrofit rest client in our application because it is fast and is significantly easier to use, also it cleaned up the codebase.

Introduction

Retrofit is a REST Client for Android and Java by Square.Retrofit turns our REST API into a Java interface.It has many features of networking and easy to learn.This open-source library provides GET,POST,PUT,DELETE  ..etc .

In Retrofit,we need to create 3 classes.

1) POJO (Plain Old Java Object) or Model Class –The Json from server cannot be use directly in Java,so we use this class to add the json retrieved from the server (e.g. Products class in our sample).

2) Interface :This is the Service class where we create an interface for managing url calls like GET,POST..etc(e.g ProductService class in our sample).Response of this call is saved into the POJO or model class.Annotations on the interface methods and its parameters indicate how a request will be handled.Every method must have an HTTP annotation that provides the request method and relative URL.

 

3) RestAdapter Class : This is ServiceGenerator Class.By default, Retrofit does the JSON parsing automatically using GSON  although you can plug in your own JSON parser if you want.It is the main part of the retrofit. We have to setup the Rest Adapter and the service.

  private static RestAdapter.Builder builder = new RestAdapter.Builder();

public static <S> S createService(Class<S> serviceClass, String baseUrl, final OAuthParameters oAuthParams) {
        OkHttpClient client = new OkHttpClient();
        client.networkInterceptors().add(new OAuthInterceptor(oAuthParams));
        builder.setClient(new OkClient(client));
        builder.setEndpoint(baseUrl).setLogLevel(RestAdapter.LogLevel.FULL);
        builder.setConverter(new SimpleXMLConverter());
        RestAdapter adapter = builder.build();
        return adapter.create(serviceClass);
    }

Here, OAuthParameters are the parameters present in the Authorization header after authorizing the consumer from service provider(Magento in our case).

Now use this  service-generator  for making Retrofit call , for e.g.

 try {
                ProductService productService = ServiceGenerator.createService(
                        ProductService.class, Constants.API_REQUEST,consumer);
                Products products = productService.getAllProducts();
                return "Products :"+products.getProduct().get(0).getName();
            }catch (Exception e){
                Log.e(TAG,e.getMessage(),e);
            }
            return null;
        }

 

Sample Source Code:

For understanding the concept more clearly,check out the code here.

Conclusion

In order to make an API call with RetroFit, we call a method on the interface, pass in any substitutions, and it will return to us a java model object.It is  easy to understand and is more friendly than other networking libraries.

References

Retrofit

RetrofitOAuth

 

 

 

 

Retrofit rest client with OAuth 1.0a in Magento for Android was last modified: August 9th, 2015 by Neha Prakash