Skip to content
Snippets Groups Projects
schemaRequest.go 1.74 KiB
Newer Older
package webdriver

import (
	"encoding/json"
	"net/http"
	"os"

	"github.com/rs/zerolog/log"
)

/*
Handles incoming schema requests

	authService: auth.UseCase, the usecase for the authentication service
	produceService: produce.UseCase, the usecase for the producer service
	Return: http.Handler, returns an http handler
*/
func (l *Listener) schemaRequestHandler(w http.ResponseWriter, r *http.Request) {
	log.
		Trace().
		Str("schemaRequestHandler", "loading headers").
		Msg("processing schema request")
	// Publish a message into the query queue
	// Grab the databaseName and cached bool from the request
	var temp map[string]interface{}
	err := json.NewDecoder(r.Body).Decode(&temp)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	log.Trace().Str("schemaRequestHandler", "processing body").Any("body", temp).Msg("processing schema request")
	databaseName, ok := temp["databaseName"].(string)
	if !ok {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	log.
		Trace().
		Str("schemaRequestHandler - Databasename", databaseName).
		Msg("processing schema request")
	cached, ok := temp["cached"].(bool)
	if !ok {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	// Pass request to schema usecase
	UserID := "UserID"
	SessionID := "SessionID"
	if os.Getenv("DEV") != "true" {
		UserID = r.Header["Userid"][0]
		SessionID = r.Header["Sessionid"][0]
	}
	body, err := json.Marshal(&temp) // json -> bytes
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	err = l.schemaService.Retrieve(&body, SessionID, UserID, databaseName, cached)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)