Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
/ Protein Public archive

💊 Protein is an IntelliJ Plugin to generate Kotlin code for Retrofit 2 and RxJava 2 based on a Swagger definition

License

Notifications You must be signed in to change notification settings

AdevintaSpain/Protein

Repository files navigation

* IntelliJ Kotlin code generator for Retrofit2 and RxJava2 based on Swagger *

Version Downloads

Protein is a plugin for intelliJ that automates the build of Retrofit "Interfaces" and "models" for Android in Kotlin. It generates the code for Retrofit based on a Swagger endpoint.


Background | Features | Download | Who Made This | Apps using Protein | Contribute | Bugs and Feedback | License


Background

Protein: A large molecule composed of one or more chains of amino acids in a specific order; the order is determined by the base sequence of nucleotides in the gene coding for the protein. Proteins are required for the structure, function, and regulation of the body’s cells, tissues, and organs, and each protein has unique functions. Examples are hormones, enzymes, and antibodies.

This scientific definition defines quite good the objective of this Plugin in an abstract way. For many time at Adevinta Spain we struggled with the idea of making a good and maintainable architecture for Android. It's been some time until we got one that fits with our needs but we still have a lot of errors and issues that human beings do every time we make manual work. That's were we came to the idea of automatization and make new components with all we need.

Protein is our approach to work better, be more productive and reliable.

NOTE: Protein is based on the library Retroswagger that is the core for the kotlin builder that parses swagger json and generates the kotlin code.

Features

  • Easy interface
  • Interface and Models auto-generated
  • Swagger integration: create code based on swagger documentation
  • IntelliJ and Android Studio compatible

Requirements

To use it you need a Swagger schema endpoint (example: http://petstore.swagger.io/v2/swagger.json).

Example

Swagger json code
{
  "swagger":"2.0",
  "info":{
    "version":"1"
  },
  "host":"localhost:8000",
  "basePath":"/",
  "tags":[
    {
      "name":"favorites-controller",
      "description":"Favorites Controller"
    }
  ],
  "paths":{
    "/favorites":{
      "get":{
        "tags":[
          "favorites-controller"
        ],
        "summary":"Get all favorites for this user",
        "operationId":"getFavorites",
        "consumes":[
          "application/json"
        ],
        "produces":[
          "application/json;charset=UTF-8"
        ],
        "parameters":[
          {
            "name":"Authorization",
            "in":"header",
            "description":"Authorization",
            "required":true,
            "type":"string"
          }
        ],
        "responses":{
          "200":{
            "description":"OK",
            "schema":{
              "$ref":"#/definitions/GetFavoritesResponse"
            }
          },
          "401":{
            "description":"Unauthorized"
          },
          "403":{
            "description":"Forbidden"
          },
          "404":{
            "description":"Not Found"
          }
        }
      }
    },
    "/favorites/{adId}":{
      "put":{
        "tags":[
          "favorites-controller"
        ],
        "summary":"Save favorite",
        "operationId":"saveFavorite",
        "consumes":[
          "application/json"
        ],
        "produces":[
          "application/json;charset=UTF-8"
        ],
        "parameters":[
          {
            "name":"adId",
            "in":"path",
            "description":"adId",
            "required":true,
            "type":"string"
          },
          {
            "name":"Authorization",
            "in":"header",
            "description":"Authorization",
            "required":true,
            "type":"string"
          }
        ],
        "responses":{
          "200":{
            "description":"OK"
          },
          "201":{
            "description":"Created"
          },
          "401":{
            "description":"Unauthorized"
          },
          "403":{
            "description":"Forbidden"
          },
          "404":{
            "description":"Not Found"
          }
        }
      },
      "delete":{
        "tags":[
          "favorites-controller"
        ],
        "summary":"Delete favorite",
        "operationId":"deleteFavorite",
        "consumes":[
          "application/json"
        ],
        "produces":[
          "application/json;charset=UTF-8"
        ],
        "parameters":[
          {
            "name":"adId",
            "in":"path",
            "description":"adId",
            "required":true,
            "type":"string"
          },
          {
            "name":"Authorization",
            "in":"header",
            "description":"Authorization",
            "required":true,
            "type":"string"
          }
        ],
        "responses":{
          "200":{
            "description":"OK"
          },
          "204":{
            "description":"No Content"
          },
          "401":{
            "description":"Unauthorized"
          },
          "403":{
            "description":"Forbidden"
          }
        }
      }
    }
  },
  "definitions":{
    "FavoriteAd":{
      "type":"object",
      "properties":{
        "adId":{
          "type":"string"
        },
        "userId":{
          "type":"string"
        }
      }
    },
    "GetFavoritesResponse":{
      "type":"object",
      "properties":{
        "favorites":{
          "type":"array",
          "items":{
            "$ref":"#/definitions/FavoriteAd"
          }
        }
      }
    },
    "Single«ResponseEntity«GetFavoritesResponse»»":{
      "type":"object"
    }
  }
}
Retrofit2 + RxJava2 Interface generated in Kotlin

It will generate the interface and the models related to that interface.

package com.mycompany.mylibrary

import io.reactivex.Completable
import io.reactivex.Single
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.PUT
import retrofit2.http.Path

interface componentNameApiInterface {
    @GET("/favorites")
    fun getFavorites(): Single<GetFavoritesResponse>

    @PUT("/favorites/{adId}")
    fun saveFavorite(@Path("adId") adId: String): Completable

    @DELETE("/favorites/{adId}")
    fun deleteFavorite(@Path("adId") adId: String): Completable
}

Download

RELEASE

You can download it through the intelliJ/AndroidStudio plugin interface or you can check: https://plugins.jetbrains.com/plugin/10206-protein--android-component-builder

Build it yourself!

  1. If you want to build it locally you need download the latest version of IntelliJ Community https://www.jetbrains.com/idea/download/#section=mac
  2. Clone this repository (git@github.com:AdevintaSpain/protein.git)
  3. Execute "RunIdea" gradle task

Who made this

Ferran Pons
Ferran Pons

Apps using Protein

The following is a list of some of the public apps using Protein and are published on the Google Play Store.

Want to add your app? Found an app that no longer works or no longer uses Protein? Please submit a pull request on GitHub to update this page!

vibbo Fotocasa

Contribute

  1. Create an issue to discuss about your idea
  2. [Fork it] (https://github.com/AdevintaSpain/protein/fork)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request
  7. Profit! ✅

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

License

Copyright 2018 Adevinta Spain S.L.

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.