mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
rel aed5503e7b regaring path issues
This commit is contained in:
@@ -318,17 +318,17 @@ func TeslaMateAPICarsChargesDetailsV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsChargesDetailsV1 returned /cars/%d/charges/%d data:", CarID, ChargeID)
|
||||
log.Printf("[debug] TeslaMateAPICarsChargesDetailsV1 returned /api/v1/cars/%d/charges/%d data:", CarID, ChargeID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsChargesDetailsV1 executed /cars/%d/charges/%d successful.", CarID, ChargeID)
|
||||
log.Printf("[info] TeslaMateAPICarsChargesDetailsV1 executed /api/v1/cars/%d/charges/%d successful.", CarID, ChargeID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsChargesDetailsV1 error in /cars/%d/charges/%d execution!", CarID, ChargeID)
|
||||
log.Printf("[error] TeslaMateAPICarsChargesDetailsV1 error in /api/v1/cars/%d/charges/%d execution!", CarID, ChargeID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsChargesDetailsV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,9 +179,9 @@ func TeslaMateAPICarsV1(c *gin.Context) {
|
||||
// print to log about request
|
||||
if gin.IsDebugging() && ValidResponse {
|
||||
if CarID > 0 {
|
||||
log.Printf("[debug] TeslaMateAPICarsV1 returned /cars/%d data:", CarID)
|
||||
log.Printf("[debug] TeslaMateAPICarsV1 returned /api/v1/cars/%d data:", CarID)
|
||||
} else {
|
||||
log.Println("[debug] TeslaMateAPICarsV1 returned /cars data:")
|
||||
log.Println("[debug] TeslaMateAPICarsV1 returned /api/v1/cars data:")
|
||||
}
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
@@ -190,18 +190,18 @@ func TeslaMateAPICarsV1(c *gin.Context) {
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
if CarID > 0 {
|
||||
log.Printf("[info] TeslaMateAPICarsV1 executed /cars/%d successful.", CarID)
|
||||
log.Printf("[info] TeslaMateAPICarsV1 executed /api/v1/cars/%d successful.", CarID)
|
||||
} else {
|
||||
log.Println("[info] TeslaMateAPICarsV1 executed /cars successful.")
|
||||
log.Println("[info] TeslaMateAPICarsV1 executed /api/v1/cars successful.")
|
||||
}
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
if CarID > 0 {
|
||||
log.Printf("[error] TeslaMateAPICarsV1 error in /cars/%d execution!", CarID)
|
||||
log.Printf("[error] TeslaMateAPICarsV1 error in /api/v1/cars/%d execution!", CarID)
|
||||
} else if len(ParamCarID) > 0 {
|
||||
log.Printf("[error] TeslaMateAPICarsV1 error in /cars/%s execution!", ParamCarID)
|
||||
log.Printf("[error] TeslaMateAPICarsV1 error in /api/v1/cars/%s execution!", ParamCarID)
|
||||
} else {
|
||||
log.Println("[error] TeslaMateAPICarsV1 error in /cars execution!")
|
||||
log.Println("[error] TeslaMateAPICarsV1 error in /api/v1/cars execution!")
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsV1.."})
|
||||
}
|
||||
|
||||
@@ -198,17 +198,17 @@ func TeslaMateAPICarsChargesV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsChargesV1 returned /cars/%d/charges data:", CarID)
|
||||
log.Printf("[debug] TeslaMateAPICarsChargesV1 returned /api/v1/cars/%d/charges data:", CarID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsChargesV1 executed /cars/%d/charges successful.", CarID)
|
||||
log.Printf("[info] TeslaMateAPICarsChargesV1 executed /api/v1/cars/%d/charges successful.", CarID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsChargesV1 error in /cars/%d/charges execution!", CarID)
|
||||
log.Printf("[error] TeslaMateAPICarsChargesV1 error in /api/v1/cars/%d/charges execution!", CarID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsChargesV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,17 +246,17 @@ func TeslaMateAPICarsDrivesV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsDrivesV1 returned /cars/%d/drives data:", CarID)
|
||||
log.Printf("[debug] TeslaMateAPICarsDrivesV1 returned /api/v1/cars/%d/drives data:", CarID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsDrivesV1 executed /cars/%d/drives successful.", CarID)
|
||||
log.Printf("[info] TeslaMateAPICarsDrivesV1 executed /api/v1/cars/%d/drives successful.", CarID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsDrivesV1 error in /cars/%d/drives execution!", CarID)
|
||||
log.Printf("[error] TeslaMateAPICarsDrivesV1 error in /api/v1/cars/%d/drives execution!", CarID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsDrivesV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,17 +381,17 @@ func TeslaMateAPICarsDrivesDetailsV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsDrivesDetailsV1 returned /cars/%d/drives/%d data:", CarID, DriveID)
|
||||
log.Printf("[debug] TeslaMateAPICarsDrivesDetailsV1 returned /api/v1/cars/%d/drives/%d data:", CarID, DriveID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsDrivesDetailsV1 executed /cars/%d/drives/%d successful.", CarID, DriveID)
|
||||
log.Printf("[info] TeslaMateAPICarsDrivesDetailsV1 executed /api/v1/cars/%d/drives/%d successful.", CarID, DriveID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsDrivesDetailsV1 error in /cars/%d/drives/%d execution!", CarID, DriveID)
|
||||
log.Printf("[error] TeslaMateAPICarsDrivesDetailsV1 error in /api/v1/cars/%d/drives/%d execution!", CarID, DriveID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsDrivesDetailsV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -548,17 +548,17 @@ func TeslaMateAPICarsStatusV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsStatusV1 returned /cars/%d/status data:", CarID)
|
||||
log.Printf("[debug] TeslaMateAPICarsStatusV1 returned /api/v1/cars/%d/status data:", CarID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsStatusV1 executed /cars/%d/status successful.", CarID)
|
||||
log.Printf("[info] TeslaMateAPICarsStatusV1 executed /api/v1/cars/%d/status successful.", CarID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsStatusV1 error in /cars/%d/status execution!", CarID)
|
||||
log.Printf("[error] TeslaMateAPICarsStatusV1 error in /api/v1/cars/%d/status execution!", CarID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsStatusV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,17 +124,17 @@ func TeslaMateAPICarsUpdatesV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Printf("[debug] TeslaMateAPICarsUpdatesV1 returned /cars/%d/updates data:", CarID)
|
||||
log.Printf("[debug] TeslaMateAPICarsUpdatesV1 returned /api/v1/cars/%d/updates data:", CarID)
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Printf("[info] TeslaMateAPICarsUpdatesV1 executed /cars/%d/updates successful.", CarID)
|
||||
log.Printf("[info] TeslaMateAPICarsUpdatesV1 executed /api/v1/cars/%d/updates successful.", CarID)
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Printf("[error] TeslaMateAPICarsUpdatesV1 error in /cars/%d/updates execution!", CarID)
|
||||
log.Printf("[error] TeslaMateAPICarsUpdatesV1 error in /api/v1/cars/%d/updates execution!", CarID)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsUpdatesV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,17 +127,17 @@ func TeslaMateAPIGlobalsettingsV1(c *gin.Context) {
|
||||
|
||||
// print to log about request
|
||||
if gin.IsDebugging() {
|
||||
log.Println("[debug] TeslaMateAPIGlobalsettingsV1 returned /cars data:")
|
||||
log.Println("[debug] TeslaMateAPIGlobalsettingsV1 returned /api/v1/globalsettings data:")
|
||||
js, _ := json.Marshal(jsonData)
|
||||
log.Printf("[debug] %s\n", js)
|
||||
}
|
||||
|
||||
// return jsonData
|
||||
if ValidResponse {
|
||||
log.Println("[info] TeslaMateAPIGlobalsettingsV1 executed /cars successful.")
|
||||
log.Println("[info] TeslaMateAPIGlobalsettingsV1 executed /api/v1/globalsettings successful.")
|
||||
c.JSON(http.StatusOK, jsonData)
|
||||
} else {
|
||||
log.Println("[error] TeslaMateAPIGlobalsettingsV1 error in /cars execution!")
|
||||
log.Println("[error] TeslaMateAPIGlobalsettingsV1 error in /api/v1/globalsettings execution!")
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPIGlobalsettingsV1.."})
|
||||
}
|
||||
}
|
||||
|
||||
+39
-25
@@ -42,36 +42,50 @@ func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// root endpoint telling API is running
|
||||
r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig.."}) })
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig..", "path": "/"})
|
||||
})
|
||||
|
||||
// TeslaMateApi v1 endpoints
|
||||
v1 := r.Group("/v1")
|
||||
// TeslaMateApi /api endpoints
|
||||
api := r.Group("/api")
|
||||
{
|
||||
v1.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi v1 runnnig.."}) })
|
||||
v1.GET("/cars", TeslaMateAPICarsV1)
|
||||
v1.GET("/cars/:CarID", TeslaMateAPICarsV1)
|
||||
v1.GET("/cars/:CarID/charges", TeslaMateAPICarsChargesV1)
|
||||
v1.GET("/cars/:CarID/charges/:ChargeID", TeslaMateAPICarsChargesDetailsV1)
|
||||
v1.GET("/cars/:CarID/drives", TeslaMateAPICarsDrivesV1)
|
||||
v1.GET("/cars/:CarID/drives/:DriveID", TeslaMateAPICarsDrivesDetailsV1)
|
||||
v1.GET("/cars/:CarID/status", TeslaMateAPICarsStatusV1)
|
||||
v1.GET("/cars/:CarID/updates", TeslaMateAPICarsUpdatesV1)
|
||||
v1.GET("/globalsettings", TeslaMateAPIGlobalsettingsV1)
|
||||
// TeslaMateApi /api root
|
||||
api.GET("/", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig..", "path": "/api"})
|
||||
})
|
||||
|
||||
// TeslaMateApi /api/v1 endpoints
|
||||
v1 := api.Group("/v1")
|
||||
{
|
||||
// TeslaMateApi /api/v1 root
|
||||
v1.GET("/", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi v1 runnnig..", "path": "/api/v1"})
|
||||
})
|
||||
v1.GET("/cars", TeslaMateAPICarsV1)
|
||||
v1.GET("/cars/:CarID", TeslaMateAPICarsV1)
|
||||
v1.GET("/cars/:CarID/charges", TeslaMateAPICarsChargesV1)
|
||||
v1.GET("/cars/:CarID/charges/:ChargeID", TeslaMateAPICarsChargesDetailsV1)
|
||||
v1.GET("/cars/:CarID/drives", TeslaMateAPICarsDrivesV1)
|
||||
v1.GET("/cars/:CarID/drives/:DriveID", TeslaMateAPICarsDrivesDetailsV1)
|
||||
v1.GET("/cars/:CarID/status", TeslaMateAPICarsStatusV1)
|
||||
v1.GET("/cars/:CarID/updates", TeslaMateAPICarsUpdatesV1)
|
||||
v1.GET("/globalsettings", TeslaMateAPIGlobalsettingsV1)
|
||||
}
|
||||
|
||||
// /api/ping endpoint
|
||||
api.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) })
|
||||
}
|
||||
|
||||
// TeslaMateApi endpoints (bofore versioning)
|
||||
r.GET("/cars", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/charges", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/charges/:ChargeID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/drives", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/drives/:DriveID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/status", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/updates", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
r.GET("/globalsettings", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) })
|
||||
|
||||
// /ping endpoint
|
||||
r.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) })
|
||||
r.GET("/cars", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/charges", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/charges/:ChargeID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/drives", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/drives/:DriveID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/status", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/cars/:CarID/updates", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
r.GET("/globalsettings", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) })
|
||||
|
||||
// run this and serve on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
|
||||
Reference in New Issue
Block a user