use request context

This commit is contained in:
Andre Medeiros 2022-05-07 15:04:35 -04:00
parent b001a51a30
commit 10d2e77c4f
5 changed files with 22 additions and 24 deletions

View file

@ -21,6 +21,8 @@ type accountNotificationsRequest struct {
} }
func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
anr := &accountNotificationsRequest{} anr := &accountNotificationsRequest{}
if err := json.NewDecoder(r.Body).Decode(anr); err != nil { if err := json.NewDecoder(r.Body).Decode(anr); err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
@ -31,8 +33,6 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request
apns := vars["apns"] apns := vars["apns"]
rid := vars["redditID"] rid := vars["redditID"]
ctx := context.Background()
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil { if err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
@ -54,12 +54,12 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request
} }
func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]
rid := vars["redditID"] rid := vars["redditID"]
ctx := context.Background()
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil { if err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
@ -85,12 +85,12 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ
} }
func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]
rid := vars["redditID"] rid := vars["redditID"]
ctx := context.Background()
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil { if err != nil {
a.errorResponse(w, r, 500, err.Error()) a.errorResponse(w, r, 500, err.Error())
@ -112,11 +112,11 @@ func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request)
} }
func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]
ctx := context.Background()
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns) dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil { if err != nil {
a.errorResponse(w, r, 422, err.Error()) a.errorResponse(w, r, 422, err.Error())
@ -206,9 +206,10 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
} }
func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
ctx := context.Background()
var acct domain.Account var acct domain.Account
if err := json.NewDecoder(r.Body).Decode(&acct); err != nil { if err := json.NewDecoder(r.Body).Decode(&acct); err != nil {

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -20,7 +19,7 @@ import (
const notificationTitle = "📣 Hello, is this thing on?" const notificationTitle = "📣 Hello, is this thing on?"
func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
d := &domain.Device{} d := &domain.Device{}
if err := json.NewDecoder(r.Body).Decode(d); err != nil { if err := json.NewDecoder(r.Body).Decode(d); err != nil {
@ -40,9 +39,9 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
} }
func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) { func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) ctx := r.Context()
ctx := context.Background() vars := mux.Vars(r)
tok := vars["apns"] tok := vars["apns"]
d, err := a.deviceRepo.GetByAPNSToken(ctx, tok) d, err := a.deviceRepo.GetByAPNSToken(ctx, tok)
@ -92,8 +91,9 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
} }
func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) { func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
ctx := context.Background()
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"]) dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
if err != nil { if err != nil {

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
@ -25,9 +24,9 @@ type notificationGenerator func(*payload.Payload)
func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) { func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) ctx := r.Context()
ctx := context.Background() vars := mux.Vars(r)
tok := vars["apns"] tok := vars["apns"]
d, err := a.deviceRepo.GetByAPNSToken(ctx, tok) d, err := a.deviceRepo.GetByAPNSToken(ctx, tok)

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -15,7 +14,7 @@ import (
) )
func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv" "strconv"
@ -44,7 +43,7 @@ type watcherCreatedResponse struct {
} }
func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]
@ -175,7 +174,7 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) {
} }
func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["watcherID"], 10, 64) id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
@ -216,7 +215,7 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
} }
func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) { func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["watcherID"], 10, 64) id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
@ -250,7 +249,7 @@ type watcherItem struct {
} }
func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]