Skip to content

Commit

Permalink
feat: add email endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taciturnaxolotl committed Oct 7, 2024
1 parent 6487659 commit 9f649ad
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func main() {
healthApiHandler := api.NewHealthApiHandler(db)
heartbeatApiHandler := api.NewHeartbeatApiHandler(userService, heartbeatService, languageMappingService)
summaryApiHandler := api.NewSummaryApiHandler(userService, summaryService)
specialApiHandler := api.NewSpecialApiHandler(userService)
metricsHandler := api.NewMetricsHandler(userService, summaryService, heartbeatService, leaderboardService, keyValueService, metricsRepository)
diagnosticsHandler := api.NewDiagnosticsApiHandler(userService, diagnosticsService)
avatarHandler := api.NewAvatarHandler()
Expand Down Expand Up @@ -291,6 +292,7 @@ func main() {

// API route registrations
summaryApiHandler.RegisterRoutes(apiRouter)
specialApiHandler.RegisterRoutes(apiRouter)
healthApiHandler.RegisterRoutes(apiRouter)
heartbeatApiHandler.RegisterRoutes(apiRouter)
metricsHandler.RegisterRoutes(apiRouter)
Expand Down
5 changes: 5 additions & 0 deletions models/special.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package models

type Email struct {
Email string `json:"email"`
}
53 changes: 53 additions & 0 deletions routes/api/special.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package api

import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/hackclub/hackatime/helpers"

conf "github.com/hackclub/hackatime/config"
"github.com/hackclub/hackatime/middlewares"
"github.com/hackclub/hackatime/services"
)

type SpecialApiHandler struct {
config *conf.Config
userSrvc services.IUserService
}

func NewSpecialApiHandler(userService services.IUserService) *SpecialApiHandler {
return &SpecialApiHandler{
userSrvc: userService,
config: conf.Get(),
}
}

func (h *SpecialApiHandler) RegisterRoutes(router chi.Router) {
r := chi.NewRouter()
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
r.Get("/", h.Get)

router.Mount("/email", r)
}

// @Summary Retrieve a users email
// @ID get-email
// @Tags email
// @Produce json
// @Param user query string false "The user to filter by if using Bearer authentication and the admin token"
// @Security ApiKeyAuth
// @Success 200 {object} models.Email
// @Router /email [get]
func (h *SpecialApiHandler) Get(w http.ResponseWriter, r *http.Request) {
user, err := h.userSrvc.GetUserById(r.URL.Query().Get("user"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

helpers.RespondJSON(w, r, http.StatusOK, map[string]interface{}{
"email": user.Email,
})
}
47 changes: 47 additions & 0 deletions static/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,39 @@ const docTemplate = `{
}
}
},
"/email": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/json"
],
"tags": [
"email"
],
"summary": "Retrieve a users email",
"operationId": "get-email",
"parameters": [
{
"type": "string",
"description": "The user to filter by if using Bearer authentication and the admin token",
"name": "user",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Email"
}
}
}
}
},
"/health": {
"get": {
"produces": [
Expand Down Expand Up @@ -1200,6 +1233,14 @@ const docTemplate = `{
}
}
},
"models.Email": {
"type": "object",
"properties": {
"email": {
"type": "string"
}
}
},
"models.Heartbeat": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1229,6 +1270,9 @@ const docTemplate = `{
"language": {
"type": "string"
},
"lines": {
"type": "integer"
},
"machine": {
"description": "ignored because wakatime api doesn't return machines currently",
"type": "string"
Expand All @@ -1240,6 +1284,9 @@ const docTemplate = `{
"project": {
"type": "string"
},
"project_root_count": {
"type": "integer"
},
"time": {
"type": "number"
},
Expand Down
47 changes: 47 additions & 0 deletions static/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,39 @@
}
}
},
"/email": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/json"
],
"tags": [
"email"
],
"summary": "Retrieve a users email",
"operationId": "get-email",
"parameters": [
{
"type": "string",
"description": "The user to filter by if using Bearer authentication and the admin token",
"name": "user",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Email"
}
}
}
}
},
"/health": {
"get": {
"produces": [
Expand Down Expand Up @@ -1192,6 +1225,14 @@
}
}
},
"models.Email": {
"type": "object",
"properties": {
"email": {
"type": "string"
}
}
},
"models.Heartbeat": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1221,6 +1262,9 @@
"language": {
"type": "string"
},
"lines": {
"type": "integer"
},
"machine": {
"description": "ignored because wakatime api doesn't return machines currently",
"type": "string"
Expand All @@ -1232,6 +1276,9 @@
"project": {
"type": "string"
},
"project_root_count": {
"type": "integer"
},
"time": {
"type": "number"
},
Expand Down
30 changes: 30 additions & 0 deletions static/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ definitions:
stacktrace:
type: string
type: object
models.Email:
properties:
email:
type: string
type: object
models.Heartbeat:
properties:
Entity:
Expand All @@ -36,6 +41,8 @@ definitions:
type: boolean
language:
type: string
lines:
type: integer
machine:
description: ignored because wakatime api doesn't return machines currently
type: string
Expand All @@ -44,6 +51,8 @@ definitions:
type: string
project:
type: string
project_root_count:
type: integer
time:
type: number
type:
Expand Down Expand Up @@ -948,6 +957,27 @@ paths:
summary: Retrieve WakaTime-compatible summaries
tags:
- wakatime
/email:
get:
operationId: get-email
parameters:
- description: The user to filter by if using Bearer authentication and the
admin token
in: query
name: user
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.Email'
security:
- ApiKeyAuth: []
summary: Retrieve a users email
tags:
- email
/health:
get:
operationId: get-health
Expand Down

0 comments on commit 9f649ad

Please sign in to comment.