All Projects → elliots → Protoc Gen Twirp_swagger

elliots / Protoc Gen Twirp_swagger

Licence: other
Swagger generator for twirp

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Protoc Gen Twirp swagger

Gf Cli
GoFrame Command Line Interface, which is your helpmate for building GoFrame application with convenience.
Stars: ✭ 143 (+164.81%)
Mutual labels:  swagger, grpc, protobuf
Go Micro Boilerplate
The boilerplate of the GoLang application with a clear microservices architecture.
Stars: ✭ 147 (+172.22%)
Mutual labels:  swagger, grpc, protobuf
Clay
Proto-first minimal server platform for gRPС+REST+Swagger APIs
Stars: ✭ 212 (+292.59%)
Mutual labels:  swagger, grpc, protobuf
Prototool
Your Swiss Army Knife for Protocol Buffers
Stars: ✭ 4,932 (+9033.33%)
Mutual labels:  grpc, protobuf
Protobuf
[Looking for new ownership] Protocol Buffers for Go with Gadgets
Stars: ✭ 4,998 (+9155.56%)
Mutual labels:  grpc, protobuf
Polyglot
A universal grpc command line client
Stars: ✭ 488 (+803.7%)
Mutual labels:  grpc, protobuf
Kroto Plus
gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Stars: ✭ 400 (+640.74%)
Mutual labels:  grpc, protobuf
Brpc Java
Java implementation for Baidu RPC, multi-protocol & high performance RPC.
Stars: ✭ 647 (+1098.15%)
Mutual labels:  grpc, protobuf
Proteus
Generate .proto files from Go source code.
Stars: ✭ 593 (+998.15%)
Mutual labels:  grpc, protobuf
Protoreflect
Reflection (Rich Descriptors) for Go Protocol Buffers
Stars: ✭ 651 (+1105.56%)
Mutual labels:  grpc, protobuf
Grpc Jersey
gRPC<->Jersey bridge
Stars: ✭ 23 (-57.41%)
Mutual labels:  grpc, protobuf
Nrpc
nRPC is like gRPC, but over NATS
Stars: ✭ 440 (+714.81%)
Mutual labels:  grpc, protobuf
Spring boot
Spring Boot 使用总结 和 demo。 如果您觉得本代码对您有所帮助,请点击页面右上方"Star"
Stars: ✭ 431 (+698.15%)
Mutual labels:  grpc, protobuf
Grpcurl
Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers
Stars: ✭ 6,149 (+11287.04%)
Mutual labels:  grpc, protobuf
Gruf
gRPC Ruby Framework
Stars: ✭ 411 (+661.11%)
Mutual labels:  grpc, protobuf
Grpclib
Pure-Python gRPC implementation for asyncio
Stars: ✭ 615 (+1038.89%)
Mutual labels:  grpc, protobuf
Rpc Thunderdome
A comparison between Proteus RPC and other commonly used RPC frameworks
Stars: ✭ 22 (-59.26%)
Mutual labels:  grpc, protobuf
Go Micro Services
HTTP up front, Protobufs in the rear
Stars: ✭ 853 (+1479.63%)
Mutual labels:  grpc, protobuf
Grpc Contract
A tool to generate the grpc server code for a contract
Stars: ✭ 40 (-25.93%)
Mutual labels:  grpc, protobuf
Grpc Example
An example of using Go gRPC and tools from the greater gRPC ecosystem together with the GoGo Protobuf Project.
Stars: ✭ 352 (+551.85%)
Mutual labels:  grpc, protobuf

Twirp Swagger Generator

NOTE: This is new and barely tested. If you find problems, please open an issue :)

Also Note: None of the clients or servers generated from swagger support protobuf. Only json.

Table of Contents

What is this?

A plugin for the awesome twirp - https://github.com/twitchtv/twirp

It is a protobuf generator that creates a swagger file for your twirp services.

This swagger file can then be used to generate documentation and clients for many many languages.

Usage

go get github.com/elliots/protoc-gen-twirp_swagger

then run as part of your usual protoc generation phase

protoc --go_out=. \
       --twirp_out=. \
       --twirp_swagger_out=. \
       ./test.proto

Other uses

You can import the swagger file into a lot of other tools and services.

If you find any good ones, add an issue so we can list them here.

Full Example

Proto file taken from the twirp example. All scripts/clients/docs are in example

Generating the swagger file

./generate-swagger.sh will create service.swagger.json

Output


{
  "swagger": "2.0",
  "info": {
    "title": "service.proto",
    "version": "version not set"
  },
  "schemes": [
    "http",
    "https"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/twirp/twitch.twirp.example.Haberdasher/MakeHat": {
      "post": {
        "summary": "MakeHat produces a hat of mysterious, randomly-selected color!",
        "operationId": "MakeHat",
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "$ref": "#/definitions/exampleHat"
            }
          }
        },
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/exampleSize"
            }
          }
        ],
        "tags": [
          "Haberdasher"
        ]
      }
    }
  },
  "definitions": {
    "exampleHat": {
      "type": "object",
      "properties": {
        "size": {
          "type": "integer",
          "format": "int32",
          "description": "The size of a hat should always be in inches."
        },
        "color": {
          "type": "string",
          "description": "The color of a hat will never be 'invisible', but other than\nthat, anything is fair game."
        },
        "name": {
          "type": "string",
          "description": "The name of a hat is it's type. Like, 'bowler', or something."
        }
      },
      "description": "A Hat is a piece of headwear made by a Haberdasher."
    },
    "exampleSize": {
      "type": "object",
      "properties": {
        "inches": {
          "type": "integer",
          "format": "int32"
        }
      },
      "description": "Size is passed when requesting a new hat to be made. It's always\nmeasured in inches."
    }
  }
}

Generating Clients

./generate-clients.sh will create clients for C#, go, java, javascript, lua, python, and ruby (many more are available)

Using the Clients

JavaScript

var Serviceproto = require('./javascript');

var devClient = new Serviceproto.ApiClient();
devClient.basePath = 'http://localhost:8080';

var api = new Serviceproto.HaberdasherApi(devClient);

var body = new Serviceproto.ExampleSize(); // {ExampleSize} 
body.inches = 20;

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ', data);
  }
};
api.makeHat(body, callback);

Python

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
# create an instance of the API class
cfg = swagger_client.Configuration()
cfg.host="http://localhost:8080"
client = swagger_client.ApiClient(cfg)
api_instance = swagger_client.HaberdasherApi(client)
body = swagger_client.ExampleSize() # ExampleSize | 
body.inches = 20

try:
    # MakeHat produces a hat of mysterious, randomly-selected color!
    api_response = api_instance.make_hat(body)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling HaberdasherApi->make_hat: %s\n" % e)

Go

Note: You almost certainly want to use the twirp client, not the swagger one. No protobuf here.


package main

import (
	"context"
	"fmt"

	"github.com/elliots/protoc-gen-twirp_swagger/example/clients/go"
)

func main() {
	cfg := swagger.NewConfiguration()
	cfg.BasePath = "http://localhost:8080"
	client := swagger.NewAPIClient(cfg)

	hat, resp, err := client.HaberdasherApi.MakeHat(context.Background(), swagger.ExampleSize{
		Inches: 20,
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("Got response code: %d hat: %v", resp.StatusCode, hat)
}

Generating documentation

./generate-documentation.sh

Spectacle

swagger-codegen (html2 language)

Generating servers

./generate-documentation.sh

Generates servers for rust and node.js.

See example/servers

Thanks

Based on: https://github.com/grpc-ecosystem/grpc-gateway/tree/master/protoc-gen-swagger (Like, 99.5% based on.)

Table of contents created by gh-md-toc

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].