1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: log requests from json response util method

This commit is contained in:
Ferdinand Mütsch 2021-04-26 21:26:47 +02:00
parent 4ab657ebd5
commit c6fd43a964
8 changed files with 9 additions and 10 deletions

View File

@ -129,7 +129,6 @@ func newDataFrom(s *models.Summary) *SummariesData {
defer wg.Done() defer wg.Done()
for i, e := range s.Languages { for i, e := range s.Languages {
data.Languages[i] = convertEntry(e, s.TotalTimeBy(models.SummaryLanguage)) data.Languages[i] = convertEntry(e, s.TotalTimeBy(models.SummaryLanguage))
} }
}(data) }(data)

View File

@ -96,7 +96,7 @@ func (h *HeartbeatApiHandler) Post(w http.ResponseWriter, r *http.Request) {
} }
} }
utils.RespondJSON(w, http.StatusCreated, constructSuccessResponse(len(heartbeats))) utils.RespondJSON(w, r, http.StatusCreated, constructSuccessResponse(len(heartbeats)))
} }
// construct weird response format (see https://github.com/wakatime/wakatime/blob/2e636d389bf5da4e998e05d5285a96ce2c181e3d/wakatime/api.py#L288) // construct weird response format (see https://github.com/wakatime/wakatime/blob/2e636d389bf5da4e998e05d5285a96ce2c181e3d/wakatime/api.py#L288)

View File

@ -51,5 +51,5 @@ func (h *SummaryApiHandler) Get(w http.ResponseWriter, r *http.Request) {
return return
} }
utils.RespondJSON(w, http.StatusOK, summary) utils.RespondJSON(w, r, http.StatusOK, summary)
} }

View File

@ -101,7 +101,7 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
cacheKey := fmt.Sprintf("%s_%v_%s_%s", user.ID, *interval, filterEntity, filterKey) cacheKey := fmt.Sprintf("%s_%v_%s_%s", user.ID, *interval, filterEntity, filterKey)
if cacheResult, ok := h.cache.Get(cacheKey); ok { if cacheResult, ok := h.cache.Get(cacheKey); ok {
utils.RespondJSON(w, http.StatusOK, cacheResult.(*v1.BadgeData)) utils.RespondJSON(w, r, http.StatusOK, cacheResult.(*v1.BadgeData))
return return
} }
@ -114,7 +114,7 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
vm := v1.NewBadgeDataFrom(summary, filters) vm := v1.NewBadgeDataFrom(summary, filters)
h.cache.SetDefault(cacheKey, vm) h.cache.SetDefault(cacheKey, vm)
utils.RespondJSON(w, http.StatusOK, vm) utils.RespondJSON(w, r, http.StatusOK, vm)
} }
func (h *BadgeHandler) loadUserSummary(user *models.User, interval *models.IntervalKey) (*models.Summary, error, int) { func (h *BadgeHandler) loadUserSummary(user *models.User, interval *models.IntervalKey) (*models.Summary, error, int) {

View File

@ -64,7 +64,7 @@ func (h *AllTimeHandler) Get(w http.ResponseWriter, r *http.Request) {
} }
vm := v1.NewAllTimeFrom(summary, models.NewFiltersWith(models.SummaryProject, values.Get("project"))) vm := v1.NewAllTimeFrom(summary, models.NewFiltersWith(models.SummaryProject, values.Get("project")))
utils.RespondJSON(w, http.StatusOK, vm) utils.RespondJSON(w, r, http.StatusOK, vm)
} }
func (h *AllTimeHandler) loadUserSummary(user *models.User) (*models.Summary, error, int) { func (h *AllTimeHandler) loadUserSummary(user *models.User) (*models.Summary, error, int) {

View File

@ -103,7 +103,7 @@ func (h *StatsHandler) Get(w http.ResponseWriter, r *http.Request) {
stats.Data.Machines = nil stats.Data.Machines = nil
} }
utils.RespondJSON(w, http.StatusOK, stats) utils.RespondJSON(w, r, http.StatusOK, stats)
} }
func (h *StatsHandler) loadUserSummary(user *models.User, start, end time.Time) (*models.Summary, error, int) { func (h *StatsHandler) loadUserSummary(user *models.User, start, end time.Time) (*models.Summary, error, int) {

View File

@ -76,7 +76,7 @@ func (h *SummariesHandler) Get(w http.ResponseWriter, r *http.Request) {
} }
vm := v1.NewSummariesFrom(summaries, filters) vm := v1.NewSummariesFrom(summaries, filters)
utils.RespondJSON(w, http.StatusOK, vm) utils.RespondJSON(w, r, http.StatusOK, vm)
} }
func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary, error, int) { func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary, error, int) {

View File

@ -6,10 +6,10 @@ import (
"net/http" "net/http"
) )
func RespondJSON(w http.ResponseWriter, status int, object interface{}) { func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status) w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(object); err != nil { if err := json.NewEncoder(w).Encode(object); err != nil {
config.Log().Error("error while writing json response: %v", err) config.Log().Request(r).Error("error while writing json response: %v", err)
} }
} }