Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added json.Controller #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added json.Controller #11

wants to merge 1 commit into from

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.

1 participant