Giter Site home page Giter Site logo

hossain-khan / medium-api-android-sample Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 196 KB

Sample android app for medium. It showcases process of generating android client library from OpenAPI specification found at https://github.com/amardeshbd/medium-api-specification.

License: MIT License

Java 100.00%
swagger-codegen openapi-specification android-sample medium-api swagger-specification

medium-api-android-sample's Introduction

  • UPDATE #1 (Mar, 2019) - Medium.com API is no longer supported.
  • UPDATE #2 (May, 2020) - Seems like Medium now allows you to create self-access tokens. API is operational again ¯\_(ツ)_/¯

Looks like Medium does not support API anymore. The post with API info has been taken down - https://blog.medium.com/welcome-to-the-medium-api-3418f956552 (Unlisted)


Build Status GitHub issues Swagger Codegen Version Used OpenAPI Specification - medium.com

Android Sample - Medium API

Sample android app for medium. It showcases process of generating Android Retrofit 2 api-client library from OpenAPI specification and how to use the ApiClient and retrofit.

DISCLAIMER: This application code has been generated using activity template provided in Android Studio. It is intended to showcase use of client library generated by swagger client. No effort has been made to follow industry's best practices for developing android apps. If you are interested in learning best practices for developing android apps, please follow Google's official guides found here: https://github.com/googlesamples/android-architecture

Library Code Gen - Retrofit

Install swagger codegen to be able to generate library specific code for certain language. For this example, we are going to generate Retrofit 2 compliant Java library for medium.com using OpenAPI specification found in medium-api-specification.

swagger-codegen generate --input-spec medium-api-specification.yaml --lang java --library retrofit2 --output medium-api-android-retrofit-client

NOTE: If you haven't installed swagger-codegen via MacOSX's brew, you may replace swagger-codegen with java -jar swagger-codegen-cli.jar to use the jar distribution.

After successfull execution, it will generate code in destination folder defined by --output directory. Here is a snapshot of generated code found in this repository.

Once code is generated, you may choose any of the available options.

  • Option 1: Copy all the files under generated source and import in android project (Used this method in current project - See apilib gradle module).
  • Option 2: You can execute gradle build to build jar file that can be included in your android project.
  • Option 3: Use their pom.xml file to host library in maven repo

Known issue on generated code:

During my testing I found bug in generated ApiClient class related to ApiKey authentication. Update following block of code to make it compatible with api key authentication.

On createDefaultAdapter() method implementation, after okBuilder is created, add the interceptors to builder.

public void createDefaultAdapter() {
    // ... more code above ...
    okBuilder = new OkHttpClient.Builder();

    // [DEV NOTE: Added custom code to add interceptors for authorizations]
    for (Map.Entry<String, Interceptor> entry : apiAuthorizations.entrySet()) {
        okBuilder.addInterceptor(entry.getValue());
    }
    // .. more code below ...
}

Here is my github gist of modified ApiClient class.

TODO Report this issue to swagger-codegen project.

Using Generated Library

Once you have generated code with modification mentioned above, you can use your generated self-issued access tokens from medium.com's user settings to access the endpoints.

Create an instance of ApiClient using following code.

    final String AUTH_ID_API_KEY = "BearerToken"; // Auth ID for "apiKey" security defined in API specification
    final String BEARER = "Bearer"; // For header based API-Key authentication
    final String TOKEN = ""; // Your self-issued access tokens
    private ApiClient apiClient;
    
    apiClient = new ApiClient(AUTH_ID_API_KEY, BEARER + " " + TOKEN);

Once you have the api client instance, you can create retrofit service class and invoke the api. See Retrofit for more info.

Here is an example of invoking /me api endpoint using asynchronous call (Source: MainActivity.java)

    UsersApi usersApi = apiClient.createService(UsersApi.class);
    Call<UserResponse> userResponseCall = usersApi.meGet();
    
    userResponseCall.enqueue(new Callback<UserResponse>() {
        @Override
        public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
            if(response.isSuccessful()) {
                User userInfo = response.body().getData();
                // Use the `userInfo` object to update UI
            } else {
                // API access denied - show message (eg. `response.errorBody().source().toString()` )
            }
        }
    
        @Override
        public void onFailure(Call<UserResponse> call, Throwable t) {
            // Network request failed.
        }
    });

Example to publications/{publicationId}/contributors API call can also be found at PublicationListActivity.java

Retrofit + RxJava

If you are interested in RxJava, you can generate retrofit+rxjava client library using the following command line

swagger-codegen generate --input-spec medium-api-specification.yaml --lang java --library retrofit2 -DuseRxJava=true --output medium-api-android-retrofit-rxjava-client

See swagger-codegen project's documentation for more hidden features ^_^

License

This project is subject to The MIT License (MIT).

medium-api-android-sample's People

Contributors

amardeshbd avatar hossain-khan avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

medium-api-android-sample's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.