0

I am using goswagger to generate my restAPI code and as part of this generated code for writing responses I should return middleware.Responder. I would like to have the option to write the response directly using the API client as I am using the gorx reactive extension, which is impossible to return a value as its running in async mode.

Code Example:

//Handle which is generated by goswagger
api.TodosFindTodosHandler = todos.FindTodosHandlerFunc(func(params todos.FindTodosParams) middleware.Responder {
        return getToListHandler(api)

    })


//goRx code which iterate over all items and handle it using observer model
  func getToListHandler(api *operations.TodoListAPI) middleware.Responder {
    watcher := observer.Observer{
        NextHandler: func(item interface{}) {
            ms, ok := item.(*models.Item)
            if ok {
                 //How can write the response here i tried this but didnt work
                result := middleware.ResponderFunc(func(rw 
                http.ResponseWriter, p runtime.Producer) {
                rw.Write([]byte("hello"))
                rw.WriteHeader(200)
            })
             //message just to use the result value to skip error
            fmt.Println("result value of method '%v'", x)
                })

            }
        },
        // Register a handler for any emitted error.
        ErrHandler: func(err error) {
             //How can write the response here
        },

        // Register a handler when a stream is completed.
        DoneHandler: func() {
         //How can write the response here
        },
    }
    it, _ := iterable.New(getAllTGoListMode())
    source := observable.From(it)
    sub := source.Subscribe(watcher)
    <-sub
    return middleware.NotImplemented("DONE....")
  } 

As part of my code I would like to write the response directly as part of the NextHandler, NextHandler, DoneHandler

Thanks

Tony.

1 Answers1

0

the middleware.Responder is a simple one method interface that gives you full access to the response and the serializers for that response content type.

You can take a look at this example: https://github.com/go-swagger/go-swagger/blob/master/examples/stream-server/restapi/configure_countdown.go#L43-L47

It streams a counter but it's the same use case.

Casual Jim
  • 1,179
  • 7
  • 13
  • But bases on this example i cant middleware.ResponderFunc i must return the value as part of my Nexthandler which is impossible , is there i way that i can write response with out return the middleware – Tony Manior Jul 01 '19 at 07:16
  • I tried the link and implement the method x := middleware.ResponderFunc with dummy string "hello" but stil not able to see it as part of the response – Tony Manior Jul 01 '19 at 07:34