Skip to content

Commit

Permalink
Add convenience method for decoding user data in custom endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
doingodswork committed Jul 11, 2020
1 parent 46bfb36 commit 6c6710c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
14 changes: 14 additions & 0 deletions addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func (a *Addon) RegisterUserData(userDataObject interface{}) {
a.userDataType = t
}

// DecodeUserData decodes the request's user data and returns the result.
// It's useful when you add custom endpoints to the addon that don't have a userData parameter
// like the ManifestCallback, CatalogHandler and StreamHandler have.
// The param value must match the URL parameter you used when creating the custom endpoint,
// for example when using `AddEndpoint("GET", "/:userData/ping", customEndpoint)` you must pass "userData".
func (a *Addon) DecodeUserData(param string, c *fiber.Ctx) (interface{}, error) {
data := c.Params(param, "")
return decodeUserData(data, a.userDataType, a.logger, a.opts.UserDataIsBase64)
}

// AddMiddleware appends a custom middleware to the chain of existing middlewares.
// Set path to an empty string or "/" to let the middleware apply to all routes.
// Don't forget to call c.Next() on the Fiber context!
Expand All @@ -132,6 +142,10 @@ func (a *Addon) AddMiddleware(path string, mw func(*fiber.Ctx)) {
}

// AddEndpoint adds a custom endpoint (a route and its handler).
// If you want to be able to access custom user data, you can use a path like this:
// "/:userData/foo" and then either deal with the data yourself
// by using `c.Params("userData", "")` in the handler,
// or use the convenience method `DecodeUserData("userData", c)`.
func (a *Addon) AddEndpoint(method, path string, handler func(*fiber.Ctx)) {
customEndpoint := customEndpoint{
method: method,
Expand Down
25 changes: 22 additions & 3 deletions examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func main() {
addon.SetManifestCallback(manifestCallback)

// Add a custom endpoint that responds to requests to /ping with "pong".
addon.AddEndpoint("GET", "/ping", customEndpoint)
customEndpoint := createCustomEndpoint(addon, logger)
addon.AddEndpoint("GET", "/:userData/ping", customEndpoint)

addon.Run()
}
Expand Down Expand Up @@ -190,6 +191,24 @@ func createManifestCallback(logger *zap.Logger) stremio.ManifestCallback {
}
}

func customEndpoint(c *fiber.Ctx) {
c.SendString("pong")
func createCustomEndpoint(addon *stremio.Addon, logger *zap.Logger) func(*fiber.Ctx) {
return func(c *fiber.Ctx) {
// We used "/:userData" when creating the custom endpoint, so we must pass that parameter name to access the custom user data.
userData, err := addon.DecodeUserData("userData", c)
if err != nil {
logger.Warn("Couldn't decode user data", zap.Error(err))
c.Status(fiber.StatusBadRequest)
return
}
u, ok := userData.(*customer)
if !ok {
t := fmt.Sprintf("%T", userData)
logger.Error("Couldn't convert user data to customer object", zap.String("type", t))
c.Status(fiber.StatusInternalServerError)
return
}
logger.Info("A user called the ping endpoint", zap.String("user", u.UserID))

c.SendString("pong")
}
}

0 comments on commit 6c6710c

Please sign in to comment.