From 8bae22715453a33a3c3e304681a6b31036aef6ef Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 19 Feb 2024 03:10:41 +0700 Subject: [PATCH] clear and update --- .gitignore | 1 + client.kt | 153 ---------------------------------------- main.go | 52 +++++++++++--- models.kt | 202 ----------------------------------------------------- 4 files changed, 42 insertions(+), 366 deletions(-) delete mode 100644 client.kt delete mode 100644 models.kt diff --git a/.gitignore b/.gitignore index 09820b0..741d3e0 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ go.work /swaggers +/res diff --git a/client.kt b/client.kt deleted file mode 100644 index b582858..0000000 --- a/client.kt +++ /dev/null @@ -1,153 +0,0 @@ -package client - -import io.micronaut.http.HttpResponse -import io.micronaut.http.annotation.* -import io.micronaut.http.client.annotation.Client - -@Client(value = "INPUT URL") -interface GDSClient { - - @Get(value = "/sales/dispatch/places") - fun getDispatchPlaces( - @Header authorization: String?, - ): HttpResponse> - - @Post(value = "/sales/order/{orderId}/passenger") - fun addPassenger( - @Header authorization: String?, - @PathVariable orderId: Int, - ): HttpResponse - - @Put(value = "/sales/session/open") - fun openSession( - @Header authorization: String?, - ): HttpResponse - - @Get(value = "/sales/trip/{tripId}/document/types") - fun getDocumentTypes( - @Header authorization: String?, - @PathVariable tripId: Int, - ): HttpResponse> - - @Get(value = "/sales/arrival/places") - fun getArrivalPlaces( - @Header authorization: String?, - ): HttpResponse> - - @Put(value = "/sales/order/tickets/{ticketId}/passenger") - fun updateTickerPassenger( - @Header authorization: String?, - @PathVariable ticketId: Int, - ): HttpResponse<> - - @Get(value = "/sales/order/{orderId}") - fun getOrder( - @Header authorization: String?, - @PathVariable orderId: Int, - ): HttpResponse - - @Delete(value = "/sales/order/{orderId}") - fun cancelOrder( - @Header authorization: String?, - @PathVariable orderId: Int, - ): HttpResponse - - @Post(value = "/sales/order/{orderId}/confirm/payment/by/{payForm}") - fun confirmPayment( - @Header authorization: String?, - @PathVariable orderId: Int, - @PathVariable payForm: String, - ): HttpResponse - - @Get(value = "/sales/ticket/series/{series}/number/{number}/return/calculation") - fun getReturnInfo( - @Header authorization: String?, - @PathVariable series: String, - @PathVariable number: String, - ): HttpResponse - - @Get(value = "/sales/trip/{tripId}/from/station/{dispatchStationId}/to/station/{arrivalStationId}/book/{ticketCount}") - fun bookOrder( - @Header authorization: String?, - @PathVariable tripId: String, - @PathVariable dispatchStationId: String, - @PathVariable arrivalStationId: String, - @PathVariable ticketCount: Int, - @PathVariable baggageCount: Int?, - ): HttpResponse - - @Get(value = "/settings") - fun getSettings( - ): HttpResponse - - @Post(value = "/auth/token") - fun login( - ): HttpResponse - - @Get(value = "/kassa") - fun index( - ): HttpResponse<> - - @Get(value = "/sales/dispatch/place/{placeId}/stations") - fun getDispatchStations( - @Header authorization: String?, - @PathVariable placeId: Int, - ): HttpResponse> - - @Post(value = "/sales/order/{orderId}/baggage") - fun addBaggage( - @Header authorization: String?, - @PathVariable orderId: Int, - ): HttpResponse - - @Put(value = "/sales/session/close") - fun closeSession( - @Header authorization: String?, - ): HttpResponse - - @Get(value = "/sales/trip/{tripId}/from/{dispatchStationId}/to/{arrivalStationId}/seats/free") - fun getSeatsFree( - @Header authorization: String?, - @PathVariable tripId: Int, - @PathVariable dispatchStationId: Int, - @PathVariable arrivalStationId: Int, - ): HttpResponse> - - @Get(value = "/sales/trip/{tripId}/ticket/types") - fun getTicketTypes( - @Header authorization: String?, - @PathVariable tripId: String, - ): HttpResponse> - - @Get(value = "/countries") - fun getCountries( - ): HttpResponse> - - @Get(value = "/sales/arrival/place/{placeId}/stations") - fun getArrivalStations( - @Header authorization: String?, - @PathVariable placeId: Int, - ): HttpResponse> - - @Get(value = "/sales/dispatch/station/{dispatchStationId}/arrival/place/{arrivalPlaceId}/{date}/trips") - fun getTrips( - @Header authorization: String?, - @PathVariable dispatchStationId: Int, - @PathVariable arrivalPlaceId: Int, - @PathVariable date: String, - ): HttpResponse> - - @Delete(value = "/sales/order/tickets/{ticketId}") - fun cancelTicket( - @Header authorization: String?, - @PathVariable ticketId: Int, - ): HttpResponse - - @Post(value = "/sales/ticket/{ticketId}/return") - fun returnTicket( - @Header authorization: String?, - @PathVariable ticketId: Int, - ): HttpResponse - -} - diff --git a/main.go b/main.go index d1ef93c..5906444 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "sort" "strings" ) @@ -92,11 +93,11 @@ func main() { res := toMicronautKotlin(config) - if err := os.WriteFile("models.kt", []byte(res["models"]), 0600); err != nil { + if err := os.WriteFile("res/models.kt", []byte(res["models"]), 0600); err != nil { panic(err) } - if err := os.WriteFile("client.kt", []byte(res["client"]), 0600); err != nil { + if err := os.WriteFile("res/client.kt", []byte(res["client"]), 0600); err != nil { panic(err) } } @@ -122,11 +123,23 @@ func toMicronautKotlin(config *config) map[string]string { sbClient.WriteString("\n") sbClient.WriteString("@Client(value = \"INPUT URL\")\n") sbClient.WriteString("interface GDSClient {\n\n") - for path, methods := range config.Paths { - for method, v := range methods { - sbClient.WriteString(fmt.Sprintf("\t@%s(value = \"%s\")\n", toMicronautKotlinMethod(method), path)) - sbClient.WriteString(fmt.Sprintf("\tfun %s(\n", v.OperationID)) + pathKeys := getSortKeys(config.Paths) + for _, path := range pathKeys { + methods := config.Paths[path] + methodKeys := getSortKeys(methods) + for _, method := range methodKeys { + v := methods[method] + + if v.Summary != "" { + sbClient.WriteString(fmt.Sprintf("\t// %s\n", v.Summary)) + } + sbClient.WriteString(fmt.Sprintf("\t@%s(value = \"%s\")\n", toMicronautKotlinMethod(method), path)) + sbClient.WriteString(fmt.Sprintf("\tfun %s(", v.OperationID)) + + if len(v.Parameters) != 0 || v.RequestBody != nil { + sbClient.WriteString("\n") + } for _, p := range v.Parameters { sbClient.WriteString("\t\t") switch p.In { @@ -139,12 +152,20 @@ func toMicronautKotlin(config *config) map[string]string { } sbClient.WriteString(fmt.Sprintf(" %s: %s,\n", p.Name, p.Schema.toKt())) } + if v.RequestBody != nil { + sbClient.WriteString(fmt.Sprintf("\t\t@Body %s: %s,\n", "body", v.RequestBody.Content.ApplicationJSON.Schema.toKt())) + } + if len(v.Parameters) != 0 || v.RequestBody != nil { + sbClient.WriteString("\t") + } - // todo body and test - - sbClient.WriteString("\t") - sbClient.WriteString(fmt.Sprintf("): HttpResponse<%s>\n", v.Responses.Num200.Content.ApplicationJSON.Schema.toKt())) - sbClient.WriteString("\n") + sbClient.WriteString(")") + + resp := v.Responses.Num200.Content.ApplicationJSON.Schema.toKt() + if resp != "" { + sbClient.WriteString(fmt.Sprintf(": HttpResponse<%s>", resp)) + } + sbClient.WriteString("\n\n") } } sbClient.WriteString("}\n\n") @@ -189,3 +210,12 @@ func refToName(ref string) string { arr := strings.Split(ref, "/") return arr[len(arr)-1] } + +func getSortKeys[T any](m map[string]T) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + sort.Strings(keys) + return keys +} diff --git a/models.kt b/models.kt deleted file mode 100644 index eddd974..0000000 --- a/models.kt +++ /dev/null @@ -1,202 +0,0 @@ -package models - -data class CountryAVS( - var id: String?, - var name: String?, -) - -data class IdentifierDocumentAVS( - var number: String, - var series: String?, - var type: DocTypeAVS, -) - -data class SessionAVS( - var id: String, -) - -data class RegionAVS( - var name: String?, - var type: String?, - var country: CountryAVS?, - var id: String?, -) - -data class Settings( - var defaultPlaceId: String, - var defaultStationId: String, -) - -data class AreaAVS( - var name: String?, - var type: String?, - var id: String?, -) - -data class AuthRequest( - var username: String, - var password: String, - var ws: String, -) - -data class PassengerAVS( - var passenger: PassengerPartPassengerAVS, - var identifierDocument: IdentifierDocumentAVS, - var benefitDocument: BenefitDocumentAVS?, - var categoryId: Int, -) - -data class PassengerPartPassengerAVS( - var citizenship: CitizenshipAVS, - var docNum: String?, - var docSeries: String?, - var docTypeId: String?, - var gender: String, - var birthDate: String?, - var id: Int?, - var lastName: String, - var middleName: String?, - var phone: String?, - var firstName: String, -) - -data class TicketCategoryAVS( - var requiredDocTypes: List?, - var id: String, - var name: String, - var price: Double, -) - -data class TicketTypeAVS( - var id: String, - var name: String, - var requiredDocTypes: List?, -) - -data class PlaceAVS( - var region: RegionAVS?, - var area: AreaAVS?, - var id: String?, - var name: String?, - var type: String?, -) - -data class StationAVS( - var id: String?, - var name: String?, - var shortName: String?, - var address: String?, -) - -data class BusSeatAVS( - var id: String, - var name: String, - var num: Int, -) - -data class DeparturesKassa( - var arrivals: List, - var place: PlaceAVS?, - var station: StationAVS?, - var time: String?, - var tripId: String?, - var priceText: String, -) - -data class DocTypeAVS( - var id: String, - var name: String, -) - -data class AuthDTO( - var token: String?, - var refreshToken: String?, - var expireAt: Int?, -) - -data class OrderAVS( - var orderId: Int, - var tickets: List, - var expirationTime: String?, - var confirmError: String?, -) - -data class CitizenshipAVS( - var id: String, - var name: String, -) - -data class CountryCode( - var name: String, - var code: String, -) - -data class RequiredDocTypeAVS( - var id: String, - var name: String, -) - -data class TicketAVS( - var category: TicketCategoryAVS?, - var number: String?, - var passenger: PassengerPartPassengerAVS?, - var seat: BusSeatAVS?, - var series: String?, - var ticketClass: String, - var benefitDocument: BenefitDocumentAVS?, - var id: String, - var identifierDocument: IdentifierDocumentAVS?, - var payments: List?, - var status: String, -) - -data class TripKassa( - var bus: BusAVS?, - var seatsText: String, - var carrier: CarrierAVS?, - var changeSeatAllow: Boolean?, - var departures: List, - var name: String?, -) - -data class BenefitDocumentAVS( - var issuer: String?, - var number: String, - var series: String?, - var type: DocTypeAVS, - var beginDate: String?, - var endDate: String?, - var issueDate: String?, -) - -data class BusAVS( - var capacity: Int?, - var freeSeats: Int?, - var info: String?, -) - -data class CarrierAVS( - var inn: String?, - var name: String?, -) - -data class ArrivalsAVS( - var price: Int?, - var station: StationAVS?, - var time: String?, -) - -data class DocumentTypeAVS( - var id: String, - var name: String, -) - -data class TicketPaymentAVS( - var amount: Double, - var from: String?, - var group: String, - var id: String, - var name: String, - var nds: String, -) -