Home

Awesome


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.

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).