Skip to content

Conversation

@BSick7
Copy link
Owner

@BSick7 BSick7 commented Jun 2, 2023

This adds a json.Controller to provide a return-style interaction with APIs.

This is how an API call would differ between Controller and Handler.

Handler

// Endpoint definition
standard.NewEndpoint("GET", "/orgs/{orgName}/stacks/{stackId}/blocks/{blockId}/webhooks", json.Handler(webhooks.List)),

// API Logic
func (wh Webhooks) List(w *json.ResponseWriter, r *json.Request) {
	BlockEndpoint(w, r, func(orgName string, stackId, blockId int64) {
		webhooks, err := wh.notificationClient.GetWebhooks(orgName, stackId, blockId)
		if err != nil {
			log.Printf("unable to get webhooks: %s", err)
			w.SendError(errors.NewApiError(nil))
			return
		}

		w.Send(webhooks)
	})
}

Controller

// Endpoint definition
standard.NewEndpoint("GET", "/orgs/{orgName}/stacks/{stackId}/blocks/{blockId}/webhooks", json.Controller(webhooks.List)),

// API Logic
func (wh Webhooks) List(r *json.Request) ([]models.Webhook, error) {
	orgName, stackId, blockId, err := BlockEndpoint(r)
	if err != nil {
		return nil, err
	}
	return wh.notificationClient.GetWebhooks(orgName, stackId, blockId)
}

@BSick7 BSick7 requested a review from ssickles June 2, 2023 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants