solidarity-service/interoperability/solidarity-api/server/openapi/api_interact.go

191 lines
5.7 KiB
Go
Raw Normal View History

2023-10-20 11:41:39 +00:00
/*
* Solidarity Mobility API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"encoding/json"
"errors"
"github.com/gorilla/mux"
"io"
"net/http"
"strings"
)
// InteractAPIController binds http requests to an api service and writes the service results to the http response
type InteractAPIController struct {
service InteractAPIServicer
errorHandler ErrorHandler
}
// InteractAPIOption for how the controller is set up.
type InteractAPIOption func(*InteractAPIController)
// WithInteractAPIErrorHandler inject ErrorHandler into controller
func WithInteractAPIErrorHandler(h ErrorHandler) InteractAPIOption {
return func(c *InteractAPIController) {
c.errorHandler = h
}
}
// NewInteractAPIController creates a default api controller
func NewInteractAPIController(s InteractAPIServicer, opts ...InteractAPIOption) Router {
controller := &InteractAPIController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the InteractAPIController
func (c *InteractAPIController) Routes() Routes {
return Routes{
"GetBookings": Route{
strings.ToUpper("Get"),
"/bookings/{bookingId}",
c.GetBookings,
},
"PatchBookings": Route{
strings.ToUpper("Patch"),
"/bookings/{bookingId}",
c.PatchBookings,
},
"PostBookings": Route{
strings.ToUpper("Post"),
"/bookings",
c.PostBookings,
},
"PostConnections": Route{
strings.ToUpper("Post"),
"/messages",
c.PostConnections,
},
}
}
// GetBookings - Retrieves an existing Booking request.
func (c *InteractAPIController) GetBookings(w http.ResponseWriter, r *http.Request) {
authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
if !authenticated {
response := ImplResponse{
Code: 401,
Body: "Unauthorized request. Check your operator / API Key.",
}
EncodeJSONResponse(response.Body, &response.Code, w)
return
}
params := mux.Vars(r)
query := r.URL.Query()
operatorParam := query.Get("operator")
bookingIdParam := params["bookingId"]
result, err := c.service.GetBookings(r.Context(), operatorParam, bookingIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// PatchBookings - Updates status of an existing Booking request.
func (c *InteractAPIController) PatchBookings(w http.ResponseWriter, r *http.Request) {
authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
if !authenticated {
response := ImplResponse{
Code: 401,
Body: "Unauthorized request. Check your operator / API Key.",
}
EncodeJSONResponse(response.Body, &response.Code, w)
return
}
params := mux.Vars(r)
query := r.URL.Query()
operatorParam := query.Get("operator")
bookingIdParam := params["bookingId"]
statusParam := query.Get("status")
messageParam := query.Get("message")
result, err := c.service.PatchBookings(r.Context(), operatorParam, bookingIdParam, BookingStatus(statusParam), messageParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// PostBookings - Create a punctual outward Booking request.
func (c *InteractAPIController) PostBookings(w http.ResponseWriter, r *http.Request) {
authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
if !authenticated {
response := ImplResponse{
Code: 401,
Body: "Unauthorized request. Check your operator / API Key.",
}
EncodeJSONResponse(response.Body, &response.Code, w)
return
}
bookingRequestParam := BookingRequest{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&bookingRequestParam); err != nil && !errors.Is(err, io.EOF) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertBookingRequestRequired(bookingRequestParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
if err := AssertBookingRequestConstraints(bookingRequestParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.PostBookings(r.Context(), bookingRequestParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// PostConnections - Send a message to the owner of a retrieved journey.
func (c *InteractAPIController) PostConnections(w http.ResponseWriter, r *http.Request) {
postConnectionsRequestParam := PostConnectionsRequest{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&postConnectionsRequestParam); err != nil && !errors.Is(err, io.EOF) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertPostConnectionsRequestRequired(postConnectionsRequestParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
if err := AssertPostConnectionsRequestConstraints(postConnectionsRequestParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.PostConnections(r.Context(), postConnectionsRequestParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}