httprpc

package
v0.0.0-...-9c8aaae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Copyright © 2019 hori-ryota <hori.ryota@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var ClientTemplate = template.Must(template.New("").Funcs(map[string]interface{}{
	"ToLowerCamel": strcase.ToLowerCamel,
	"ToUpperCamel": strcase.ToUpperCamel,
	"ToURLPath":    httprpc.URLPath,
}).Parse(strings.TrimSpace(`
// Code generated by go-codegen api protobuf kotlin_client httprpc; DO NOT EDIT

{{if .StructdefPackage}}import {{.StructdefPackage}}.*
{{end -}}
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.request.url
import io.ktor.client.response.HttpResponse
import io.ktor.client.response.readBytes
import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.coroutines.*
import kotlinx.serialization.protobuf.ProtoBuf

class {{.KotlinClassName}}(private val urlBase: String, private val client: HttpClient, private val defaultCoroutineScope: CoroutineScope) {

{{- range .Services}}
{{$service := .}}
{{- range .RPCs}}
    fun {{ToLowerCamel .Name}}(input: {{ToUpperCamel .InputType.Obj.Name}}, coroutineScope: CoroutineScope = defaultCoroutineScope): {{if .OutputType}}Deferred<{{ToUpperCamel .OutputType.Obj.Name}}>{{else}}Job{{end}} {
        return coroutineScope.{{if .OutputType}}async{{else}}launch{{end}} {
            val protoData = ProtoBuf.dump({{ToUpperCamel .InputType.Obj.Name}}.serializer(), input)
            val url = "$urlBase/{{ToURLPath $service.Obj.Name .Name}}"
            val contentType = ContentType("application", "protobuf")
            {{- if .OutputType}}
            val response = client.post<HttpResponse> {
                url(url)
                contentType(contentType)
                body = protoData
            }
            // TODO handle error
            ProtoBuf.load({{ToUpperCamel .OutputType.Obj.Name}}.serializer(), response.readBytes())
            {{- else}}
            client.post<HttpResponse> {
                url(url)
                contentType(contentType)
                body = protoData
            }
            // TODO handle error
            {{- end}}
        }
    }
{{- end}}
{{- end}}
}
`)))

Functions

func Generate

func Generate(
	usecasePkgInfo *loader.PackageInfo,
	kotlinPackage string,
	kotlinClassName string,
	structdefPackage string,
) (string, error)

func NewHttprpcCmd

func NewHttprpcCmd() *cobra.Command

func Run

func Run(
	usecaseDir string,
	outputDir string,
	kotlinPackage string,
	kotlinClassName string,
	structdefPackage string,
	useStdOut bool,
) error

Types

type RPC

type RPC struct {
	*types.Func
	InputType  *types.Named
	OutputType *types.Named
}

func ParseRPC

func ParseRPC(method *types.Func) (*RPC, error)

type Service

type Service struct {
	*types.Named
	RPCs []RPC
}

type TemplateParam

type TemplateParam struct {
	KotlinPackage    string
	KotlinClassName  string
	StructdefPackage string
	Services         []Service
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL