list workflows endpoint

This commit is contained in:
Victor Vrantchan
2016-05-09 10:32:40 -04:00
parent 4e689c8347
commit cdbe56e373
3 changed files with 69 additions and 2 deletions

View File

@@ -52,7 +52,7 @@ func (svc workflowService) CreateWorkflow(wfRequest *Workflow) (*Workflow, error
func (svc workflowService) ListWorkflows() ([]Workflow, error) {
svc.debug.Log("Listing Workflows")
return nil, nil
return svc.db.GetWorkflows()
}
// NewService creates a new MDM Command Service
@@ -95,7 +95,17 @@ func ServiceHandler(ctx context.Context, svc Service) http.Handler {
encodeResponse,
commonOptions...,
)
listWorkflowsHandler := httptransport.NewServer(
ctx,
makeListWorkflowsEndpoint(svc),
decodeListWorkflowsRequest,
encodeResponse,
commonOptions...,
)
r := mux.NewRouter()
r.Methods("POST").Path("/mdm/workflows").Handler(newWorkflowHandler)
r.Handle("/mdm/workflows", newWorkflowHandler).Methods("POST")
r.Handle("/mdm/workflows", listWorkflowsHandler).Methods("GET")
return r
}

View File

@@ -43,6 +43,31 @@ func (r NewWorkflowResponse) status() int { return 201 }
func (r NewWorkflowResponse) error() error { return r.Err }
func decodeListWorkflowsRequest(r *http.Request) (interface{}, error) {
return nil, nil
}
// ListWorkflowsResponse is the response struct for a ListWorkflows request
type ListWorkflowsResponse struct {
workflowList []Workflow
Err error `json:"error,omitempty"`
}
func (r ListWorkflowsResponse) error() error { return r.Err }
func (r ListWorkflowsResponse) encodeList(w http.ResponseWriter) error {
jsn, err := json.MarshalIndent(r.workflowList, "", " ")
if err != nil {
return err
}
w.Write(jsn)
return nil
}
type listEncoder interface {
encodeList(w http.ResponseWriter) error
}
// errorer is implemented by all concrete response types. It allows us to
// change the HTTP response code without needing to trigger an endpoint
// (transport-level) error. For more information, read the big comment in
@@ -66,6 +91,13 @@ func encodeResponse(w http.ResponseWriter, response interface{}) error {
if e, ok := response.(statuser); ok {
w.WriteHeader(e.status())
}
// check if this is a collection
if e, ok := response.(listEncoder); ok {
return e.encodeList(w)
}
jsn, err := json.MarshalIndent(response, "", " ")
if err != nil {
return err
@@ -112,3 +144,13 @@ func makeNewWorkflowEndpoint(svc Service) endpoint.Endpoint {
return NewWorkflowResponse{Workflow: workflow}, nil
}
}
func makeListWorkflowsEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
workflows, err := svc.ListWorkflows()
if err != nil {
return ListWorkflowsResponse{Err: err}, nil
}
return ListWorkflowsResponse{workflowList: workflows}, nil
}
}

View File

@@ -132,3 +132,18 @@ func TestHTTPCreateWorkflow(t *testing.T) {
io.Copy(os.Stdout, resp.Body)
}
}
func TestHTTPListWorkflows(t *testing.T) {
req, err := client.NewRequest("workflows", "", jsonMedia, "GET")
if err != nil {
t.Fatal(err)
}
resp, err := client.Do(req, nil)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Error("Expected", http.StatusOK, "got", resp.StatusCode)
io.Copy(os.Stdout, resp.Body)
}
}