MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / store

Method store

api/pkg/handlers/phone_api_key_handler.go:110–148  ·  view source on GitHub ↗

@Summary Store phone API key @Description Creates a new phone API key which can be used to log in to the httpSMS app on your Android phone @Security ApiKeyAuth @Tags PhoneAPIKeys @Accept json @Produce json @Param payload body requests.PhoneAPIKeyStoreRequest tr

(c fiber.Ctx)

Source from the content-addressed store, hash-verified

108// @Failure 500 {object} responses.InternalServerError
109// @Router /phone-api-keys [post]
110func (h *PhoneAPIKeyHandler) store(c fiber.Ctx) error {
111 ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
112 defer span.End()
113
114 userID := h.userIDFomContext(c)
115
116 result, err := h.entitlementService.Check(ctx, userID, entities.EntityNamePhoneAPIKey, func() (int, error) {
117 return h.service.CountByUser(ctx, userID)
118 })
119 if err != nil {
120 ctxLogger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot check entitlement for phone API keys for user [%s]", userID)))
121 return h.responseInternalServerError(c)
122 }
123 if !result.Allowed {
124 return h.responsePaymentRequired(c, result.Message)
125 }
126
127 var request requests.PhoneAPIKeyStoreRequest
128 if err := c.Bind().Body(&request); err != nil {
129 msg := fmt.Sprintf("cannot marshall params [%s] into %T", c.OriginalURL(), request)
130 ctxLogger.Warn(stacktrace.Propagate(err, msg))
131 return h.responseBadRequest(c, err)
132 }
133
134 if errors := h.validator.ValidateStore(ctx, request.Sanitize()); len(errors) != 0 {
135 msg := fmt.Sprintf("validation errors [%s], while updating phones [%+#v]", spew.Sdump(errors), request)
136 ctxLogger.Warn(stacktrace.NewError(msg))
137 return h.responseUnprocessableEntity(c, errors, "validation errors while updating phones")
138 }
139
140 phoneAPIKey, err := h.service.Create(ctx, h.userFromContext(c), request.Name)
141 if err != nil {
142 msg := fmt.Sprintf("cannot update phones with params [%+#v]", request)
143 ctxLogger.Error(stacktrace.Propagate(err, msg))
144 return h.responseInternalServerError(c)
145 }
146
147 return h.responseOK(c, "phone API key created successfully", phoneAPIKey)
148}
149
150// @Summary Delete a phone API key from the database.
151// @Description Delete a phone API Key from the database and cannot be used for authentication anymore.

Callers

nothing calls this directly

Calls 15

SanitizeMethod · 0.95
userIDFomContextMethod · 0.80
CheckMethod · 0.80
responseBadRequestMethod · 0.80
userFromContextMethod · 0.80
responseOKMethod · 0.80
CountByUserMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected