Esempio n. 1
0
func MarshalResponseMiddleware(registry kit.Registry, request kit.Request, response kit.Response) (kit.Response, bool) {
	js, err := json.Marshal(response.GetData())
	if err != nil {
		js = []byte(`{"errors": [{code: "response_marshal_error"}]}`)
	}

	response.SetRawData(js)

	return nil, false
}
Esempio n. 2
0
func convertResponse(response kit.Response) *turnpike.CallResult {
	result := &turnpike.CallResult{}

	if response.GetError() != nil {
		result.Err = turnpike.URI(response.GetError().GetCode())
	}

	result.Kwargs = map[string]interface{}{
		"data":   response.GetData(),
		"meta":   response.GetMeta(),
		"errors": []error{response.GetError()},
	}

	return result
}
Esempio n. 3
0
// SerializeResponse converts a response with model data into the target format.
func (s *Serializer) SerializeResponse(response kit.Response) (interface{}, apperror.Error) {
	transData := response.GetTransferData()
	if transData == nil {
		transData = &kit.AppTransferData{}

		data := response.GetData()
		if model, ok := data.(kit.Model); ok {
			transData.SetModels([]kit.Model{model})
		} else if models, ok := data.([]kit.Model); ok {
			transData.SetModels(models)
		} else {
			transData.SetData(data)
		}
	}

	// Append response metadata.
	if meta := response.GetMeta(); meta != nil {
		transMeta := transData.GetMeta()
		if transMeta != nil {
			for key, val := range meta {
				transMeta[key] = val
			}
		} else {
			transData.SetMeta(meta)
		}
	}

	// If a response error is defined, prepend it to all errors that might have been in
	// TransferData.
	if err := response.GetError(); err != nil {
		oldErrs := transData.GetErrors()
		transData.SetErrors(append([]apperror.Error{err}, oldErrs...))
	}

	data, err := s.SerializeTransferData(transData)
	if err != nil {
		return nil, err
	}

	return data, nil
}
Esempio n. 4
0
func RespondWithJson(w http.ResponseWriter, response kit.Response) {
	code := 200
	respData := map[string]interface{}{
		"data": response.GetData(),
	}

	if response.GetError() != nil {
		errs := []error{response.GetError()}

		additionalErrs := response.GetError().GetErrors()
		if additionalErrs != nil {
			for _, err := range additionalErrs {
				if apiErr, ok := err.(apperror.Error); ok && apiErr.IsPublic() {
					errs = append(errs, apiErr)
				}
			}
		}

		respData["errors"] = errs
		code = 500
	}

	output, err2 := json.Marshal(respData)
	if err2 != nil {
		code = 500
		respData = map[string]interface{}{
			"errors": []error{
				apperror.Wrap(err2, "json_encode_error"),
			},
		}
		output, _ = json.Marshal(respData)
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	w.Write(output)
}
Esempio n. 5
0
func (f *Frontend) registerMethod(method kit.Method) {
	f.client.Register(method.GetName(), func(args []interface{}, kwargs map[string]interface{}, details map[string]interface{}) (result *turnpike.CallResult) {
		methodName := method.GetName()
		fmt.Printf("WAMP method %v |\n data: %v |\n details: %v\n", methodName, kwargs, details)

		request := kit.NewRequest()
		request.SetFrontend("wamp")
		request.SetPath("/method/" + methodName)
		request.SetData(kwargs)

		var response kit.Response

		// Find session.
		sessionId := uint(details["session_id"].(turnpike.ID))
		session := f.sessions[sessionId]
		if session == nil {
			s, err := f.registry.UserService().StartSession(nil, "wamp")
			if err != nil {
				response = kit.NewErrorResponse(err)
			} else {
				f.sessions[sessionId] = s
				session = s
			}
		}

		request.SetSession(session)
		if session.GetUser() != nil {
			request.SetUser(session.GetUser())
		}

		// Run before middlewares.
		for _, middleware := range f.beforeMiddlewares {
			resp, skip := middleware(f.registry, request)
			if skip {
				panic("WAMP frontend middlewares do not support skipping.")
			} else if resp != nil {
				response = resp
				break
			}
		}

		// Run the method.
		if response == nil {
			responder := func(r kit.Response) {
				response = r
			}

			finishedChannel, err := f.registry.App().RunMethod(methodName, request, responder, true)
			if err != nil {
				response = kit.NewErrorResponse(err)
			} else {
				<-finishedChannel
			}
		}

		// Run after middlewares.
		for _, middleware := range f.afterMiddlewares {
			resp, skip := middleware(f.registry, request, response)
			if skip {
				panic("WAMP frontend middlewares do not support skipping.")
			}
			if resp != nil {
				response = resp
			}
		}

		return &turnpike.CallResult{Kwargs: response.GetData().(map[string]interface{})}
	}, nil)
}