Skip to content

Commit 3282d9f

Browse files
feat: add apikey endpoint
1 parent 04481b2 commit 3282d9f

File tree

7 files changed

+276
-19
lines changed

7 files changed

+276
-19
lines changed

models/special.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ type Email struct {
77
type HasData struct {
88
HasData bool `json:"hasData"`
99
}
10+
11+
type ApiKey struct {
12+
ApiKey string `json:"apiKey"`
13+
}

routes/api/special.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (h *SpecialApiHandler) RegisterRoutes(router chi.Router) {
2727
r := chi.NewRouter()
2828
r.Use(middlewares.NewAuthenticateMiddleware(h.userSrvc).Handler)
2929
r.Get("/email", h.GetEmail)
30+
r.Get("/apikey", h.GetApiKey)
3031
r.Get("/hasData", h.HasData)
3132

3233
router.Mount("/special", r)
@@ -48,11 +49,32 @@ func (h *SpecialApiHandler) GetEmail(w http.ResponseWriter, r *http.Request) {
4849
return
4950
}
5051

51-
helpers.RespondJSON(w, r, http.StatusOK, map[string]interface{}{
52+
helpers.RespondJSON(w, r, http.StatusOK, map[string]any{
5253
"email": user.Email,
5354
})
5455
}
5556

57+
// @Summary Retrieve a users API key
58+
// @ID get-apikey
59+
// @Tags apikey
60+
// @Produce json
61+
// @Param user query string false "The user to filter by if using Bearer authentication and the admin token"
62+
// @Security ApiKeyAuth
63+
// @Success 200 {object} models.ApiKey
64+
// @Router /special/apikey [get]
65+
func (h *SpecialApiHandler) GetApiKey(w http.ResponseWriter, r *http.Request) {
66+
user, err := h.userSrvc.GetUserById(r.URL.Query().Get("user"))
67+
if err != nil {
68+
w.WriteHeader(http.StatusInternalServerError)
69+
w.Write([]byte(err.Error()))
70+
return
71+
}
72+
73+
helpers.RespondJSON(w, r, http.StatusOK, map[string]any{
74+
"apiKey": user.ApiKey,
75+
})
76+
}
77+
5678
// @Summary Whether the user has data any heartbeats received yet
5779
// @ID has-data
5880
// @Tags hasData
@@ -69,7 +91,7 @@ func (h *SpecialApiHandler) HasData(w http.ResponseWriter, r *http.Request) {
6991
return
7092
}
7193

72-
helpers.RespondJSON(w, r, http.StatusOK, map[string]interface{}{
94+
helpers.RespondJSON(w, r, http.StatusOK, map[string]any{
7395
"hasData": user.HasData,
7496
})
7597
}

static/assets/css/app.dist.v0.1.6.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
27 Bytes
Binary file not shown.

static/docs/docs.go

+93-6
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,39 @@ const docTemplate = `{
860860
}
861861
}
862862
},
863+
"/special/apikey": {
864+
"get": {
865+
"security": [
866+
{
867+
"ApiKeyAuth": []
868+
}
869+
],
870+
"produces": [
871+
"application/json"
872+
],
873+
"tags": [
874+
"apikey"
875+
],
876+
"summary": "Retrieve a users API key",
877+
"operationId": "get-apikey",
878+
"parameters": [
879+
{
880+
"type": "string",
881+
"description": "The user to filter by if using Bearer authentication and the admin token",
882+
"name": "user",
883+
"in": "query"
884+
}
885+
],
886+
"responses": {
887+
"200": {
888+
"description": "OK",
889+
"schema": {
890+
"$ref": "#/definitions/models.ApiKey"
891+
}
892+
}
893+
}
894+
}
895+
},
863896
"/special/email": {
864897
"get": {
865898
"security": [
@@ -960,7 +993,8 @@ const docTemplate = `{
960993
"last_year",
961994
"any",
962995
"all_time",
963-
"low_skies"
996+
"low_skies",
997+
"high_seas"
964998
],
965999
"type": "string",
9661000
"description": "Interval identifier",
@@ -1241,6 +1275,14 @@ const docTemplate = `{
12411275
}
12421276
},
12431277
"definitions": {
1278+
"models.ApiKey": {
1279+
"type": "object",
1280+
"properties": {
1281+
"apiKey": {
1282+
"type": "string"
1283+
}
1284+
}
1285+
},
12441286
"models.Diagnostics": {
12451287
"type": "object",
12461288
"properties": {
@@ -1286,9 +1328,6 @@ const docTemplate = `{
12861328
"models.Heartbeat": {
12871329
"type": "object",
12881330
"properties": {
1289-
"Entity": {
1290-
"type": "string"
1291-
},
12921331
"branch": {
12931332
"type": "string"
12941333
},
@@ -1299,19 +1338,37 @@ const docTemplate = `{
12991338
"description": "https://gorm.io/docs/conventions.html#CreatedAt",
13001339
"type": "number"
13011340
},
1341+
"cursorpos": {
1342+
"type": "integer"
1343+
},
1344+
"dependencies": {
1345+
"type": "array",
1346+
"items": {
1347+
"type": "string"
1348+
}
1349+
},
13021350
"editor": {
13031351
"description": "ignored because editor might be parsed differently by wakatime",
13041352
"type": "string"
13051353
},
1306-
"id": {
1307-
"type": "integer"
1354+
"entity": {
1355+
"type": "string"
13081356
},
13091357
"is_write": {
13101358
"type": "boolean"
13111359
},
13121360
"language": {
13131361
"type": "string"
13141362
},
1363+
"line_additions": {
1364+
"type": "integer"
1365+
},
1366+
"line_deletions": {
1367+
"type": "integer"
1368+
},
1369+
"lineno": {
1370+
"type": "integer"
1371+
},
13151372
"lines": {
13161373
"type": "integer"
13171374
},
@@ -1504,6 +1561,18 @@ const docTemplate = `{
15041561
"created_at": {
15051562
"type": "string"
15061563
},
1564+
"cursorpos": {
1565+
"type": "integer"
1566+
},
1567+
"dependencies": {
1568+
"type": "array",
1569+
"items": {
1570+
"type": "string"
1571+
}
1572+
},
1573+
"editor": {
1574+
"type": "string"
1575+
},
15071576
"entity": {
15081577
"type": "string"
15091578
},
@@ -1516,12 +1585,30 @@ const docTemplate = `{
15161585
"language": {
15171586
"type": "string"
15181587
},
1588+
"line_additions": {
1589+
"type": "integer"
1590+
},
1591+
"line_deletions": {
1592+
"type": "integer"
1593+
},
1594+
"lineno": {
1595+
"type": "integer"
1596+
},
1597+
"lines": {
1598+
"type": "integer"
1599+
},
15191600
"machine_name_id": {
15201601
"type": "string"
15211602
},
1603+
"operating_system": {
1604+
"type": "string"
1605+
},
15221606
"project": {
15231607
"type": "string"
15241608
},
1609+
"project_root_count": {
1610+
"type": "integer"
1611+
},
15251612
"time": {
15261613
"type": "number"
15271614
},

static/docs/swagger.json

+93-6
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,39 @@
852852
}
853853
}
854854
},
855+
"/special/apikey": {
856+
"get": {
857+
"security": [
858+
{
859+
"ApiKeyAuth": []
860+
}
861+
],
862+
"produces": [
863+
"application/json"
864+
],
865+
"tags": [
866+
"apikey"
867+
],
868+
"summary": "Retrieve a users API key",
869+
"operationId": "get-apikey",
870+
"parameters": [
871+
{
872+
"type": "string",
873+
"description": "The user to filter by if using Bearer authentication and the admin token",
874+
"name": "user",
875+
"in": "query"
876+
}
877+
],
878+
"responses": {
879+
"200": {
880+
"description": "OK",
881+
"schema": {
882+
"$ref": "#/definitions/models.ApiKey"
883+
}
884+
}
885+
}
886+
}
887+
},
855888
"/special/email": {
856889
"get": {
857890
"security": [
@@ -952,7 +985,8 @@
952985
"last_year",
953986
"any",
954987
"all_time",
955-
"low_skies"
988+
"low_skies",
989+
"high_seas"
956990
],
957991
"type": "string",
958992
"description": "Interval identifier",
@@ -1233,6 +1267,14 @@
12331267
}
12341268
},
12351269
"definitions": {
1270+
"models.ApiKey": {
1271+
"type": "object",
1272+
"properties": {
1273+
"apiKey": {
1274+
"type": "string"
1275+
}
1276+
}
1277+
},
12361278
"models.Diagnostics": {
12371279
"type": "object",
12381280
"properties": {
@@ -1278,9 +1320,6 @@
12781320
"models.Heartbeat": {
12791321
"type": "object",
12801322
"properties": {
1281-
"Entity": {
1282-
"type": "string"
1283-
},
12841323
"branch": {
12851324
"type": "string"
12861325
},
@@ -1291,19 +1330,37 @@
12911330
"description": "https://gorm.io/docs/conventions.html#CreatedAt",
12921331
"type": "number"
12931332
},
1333+
"cursorpos": {
1334+
"type": "integer"
1335+
},
1336+
"dependencies": {
1337+
"type": "array",
1338+
"items": {
1339+
"type": "string"
1340+
}
1341+
},
12941342
"editor": {
12951343
"description": "ignored because editor might be parsed differently by wakatime",
12961344
"type": "string"
12971345
},
1298-
"id": {
1299-
"type": "integer"
1346+
"entity": {
1347+
"type": "string"
13001348
},
13011349
"is_write": {
13021350
"type": "boolean"
13031351
},
13041352
"language": {
13051353
"type": "string"
13061354
},
1355+
"line_additions": {
1356+
"type": "integer"
1357+
},
1358+
"line_deletions": {
1359+
"type": "integer"
1360+
},
1361+
"lineno": {
1362+
"type": "integer"
1363+
},
13071364
"lines": {
13081365
"type": "integer"
13091366
},
@@ -1496,6 +1553,18 @@
14961553
"created_at": {
14971554
"type": "string"
14981555
},
1556+
"cursorpos": {
1557+
"type": "integer"
1558+
},
1559+
"dependencies": {
1560+
"type": "array",
1561+
"items": {
1562+
"type": "string"
1563+
}
1564+
},
1565+
"editor": {
1566+
"type": "string"
1567+
},
14991568
"entity": {
15001569
"type": "string"
15011570
},
@@ -1508,12 +1577,30 @@
15081577
"language": {
15091578
"type": "string"
15101579
},
1580+
"line_additions": {
1581+
"type": "integer"
1582+
},
1583+
"line_deletions": {
1584+
"type": "integer"
1585+
},
1586+
"lineno": {
1587+
"type": "integer"
1588+
},
1589+
"lines": {
1590+
"type": "integer"
1591+
},
15111592
"machine_name_id": {
15121593
"type": "string"
15131594
},
1595+
"operating_system": {
1596+
"type": "string"
1597+
},
15141598
"project": {
15151599
"type": "string"
15161600
},
1601+
"project_root_count": {
1602+
"type": "integer"
1603+
},
15171604
"time": {
15181605
"type": "number"
15191606
},

0 commit comments

Comments
 (0)