https://github.com/hossain-khan/medium-api-android-sample
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.
https://github.com/hossain-khan/medium-api-android-sample
android-sample medium-api openapi-specification swagger-codegen swagger-specification
Last synced: 8 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/hossain-khan/medium-api-android-sample
- Owner: hossain-khan
- License: mit
- Created: 2016-06-26T23:54:48.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-09-13T01:46:37.000Z (over 1 year ago)
- Last Synced: 2025-04-11T22:51:30.437Z (12 months ago)
- Topics: android-sample, medium-api, openapi-specification, swagger-codegen, swagger-specification
- Language: Java
- Homepage:
- Size: 226 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
* **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)_
-----
[](https://travis-ci.org/amardeshbd/medium-api-android-sample) [](https://github.com/amardeshbd/medium-api-android-sample/issues) [](https://github.com/swagger-api/swagger-codegen) [](https://github.com/amardeshbd/medium-api-specification/blob/master/medium-api-specification.yaml)
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](https://github.com/googlesamples/android-architecture)_
Library Code Gen - Retrofit
--------------------------------
Install [swagger codegen](https://github.com/swagger-api/swagger-codegen) to be able to generate library specific code for certain language. For this example, we are going to generate **[Retrofit 2](http://square.github.io/retrofit/)** compliant Java library for [medium.com](https://github.com/Medium/medium-api-docs) using OpenAPI specification found in [medium-api-specification](https://github.com/amardeshbd/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](https://github.com/amardeshbd/medium-api-android-sample/tree/master/apilib/src/main/java/io/swagger/client)** 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.
```java
public void createDefaultAdapter() {
// ... more code above ...
okBuilder = new OkHttpClient.Builder();
// [DEV NOTE: Added custom code to add interceptors for authorizations]
for (Map.Entry entry : apiAuthorizations.entrySet()) {
okBuilder.addInterceptor(entry.getValue());
}
// .. more code below ...
}
```
Here is my **[github gist](https://gist.github.com/amardeshbd/063213c29ebc5bf98ff071df0c22a44c)** 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](https://github.com/Medium/medium-api-docs#22-self-issued-access-tokens) from medium.com's [user settings](https://medium.com/me/settings) to access the endpoints.
Create an instance of `ApiClient` using following code.
```java
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](http://square.github.io/retrofit/) for more info.
Here is an example of invoking `/me` api endpoint using asynchronous call _(Source: [MainActivity.java](https://github.com/amardeshbd/medium-api-android-sample/blob/master/app/src/main/java/info/hossainkhan/mediumsample/MainActivity.java))_
```java
UsersApi usersApi = apiClient.createService(UsersApi.class);
Call userResponseCall = usersApi.meGet();
userResponseCall.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response 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 call, Throwable t) {
// Network request failed.
}
});
```
Example to `publications/{publicationId}/contributors` API call can also be found at _[PublicationListActivity.java](https://github.com/amardeshbd/medium-api-android-sample/blob/master/app/src/main/java/info/hossainkhan/mediumsample/PublicationListActivity.java)_
Retrofit + RxJava
-------------------
If you are interested in [RxJava](https://github.com/ReactiveX/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).