Move API models and errors to separate files

This commit is contained in:
Maurizio Porrato 2021-05-17 17:23:39 +01:00
parent e99ccdab9c
commit 025f74808a
Signed by: guru
GPG Key ID: C622977DF024AC24
4 changed files with 58 additions and 57 deletions

View File

@ -8,7 +8,6 @@ import (
"net/http"
"regexp"
"strings"
"time"
"gitlab.com/mporrato/uBrowserSync/syncstore"
)
@ -32,34 +31,6 @@ var (
http.StatusBadRequest)
)
type serviceInfoResp struct {
Version string `json:"version"`
Message string `json:"message"`
MaxSyncSize int `json:"maxSyncSize"`
Status int `json:"status"`
}
type CreateReq struct {
Version string `json:"version"`
}
type UpdateReq struct {
Bookmarks string `json:"bookmarks"`
LastUpdated time.Time `json:"lastUpdated"`
}
type UpdateResp struct {
LastUpdated time.Time `json:"lastUpdated"`
}
type LastUpdatedResp struct {
LastUpdated time.Time `json:"lastUpdated"`
}
type GetSyncVerResp struct {
Version string `json:"version"`
}
func sendJSON(w http.ResponseWriter, status int, data interface{}) {
body, err := json.Marshal(data)
if err != nil {
@ -95,7 +66,7 @@ func info(w http.ResponseWriter, req *http.Request) {
sendJSONError(w, syncstore.NotImplementedError)
} else {
log.Println("info()")
serviceInfo := serviceInfoResp{
serviceInfo := syncstore.ServiceInfoResp{
Version: apiVersion,
Message: infoMessage,
Status: serviceStatus,
@ -105,7 +76,7 @@ func info(w http.ResponseWriter, req *http.Request) {
}
func createSync(w http.ResponseWriter, req *http.Request) {
body := new(CreateReq)
body := new(syncstore.CreateReq)
err := json.NewDecoder(req.Body).Decode(&body)
if err != nil {
sendJSONError(w, invalidRequestError)
@ -138,7 +109,7 @@ func getLastUpdated(syncId string, w http.ResponseWriter, _ *http.Request) {
sendJSONError(w, err)
return
}
sendJSONOk(w, LastUpdatedResp{LastUpdated: resp})
sendJSONOk(w, syncstore.LastUpdatedResp{LastUpdated: resp})
}
func getVersion(syncId string, w http.ResponseWriter, _ *http.Request) {
@ -147,11 +118,11 @@ func getVersion(syncId string, w http.ResponseWriter, _ *http.Request) {
sendJSONError(w, err)
return
}
sendJSONOk(w, GetSyncVerResp{Version: resp})
sendJSONOk(w, syncstore.GetSyncVerResp{Version: resp})
}
func updateSync(syncId string, w http.ResponseWriter, req *http.Request) {
body := new(UpdateReq)
body := new(syncstore.UpdateReq)
err := json.NewDecoder(req.Body).Decode(&body)
if err != nil {
sendJSONError(w, invalidRequestError)
@ -166,7 +137,7 @@ func updateSync(syncId string, w http.ResponseWriter, req *http.Request) {
sendJSONError(w, err)
return
}
sendJSONOk(w, UpdateResp{LastUpdated: resp})
sendJSONOk(w, syncstore.UpdateResp{LastUpdated: resp})
}
func bookmarks(w http.ResponseWriter, req *http.Request) {

View File

@ -3,18 +3,8 @@ package syncstore
import (
"fmt"
"net/http"
"time"
)
type Blob struct {
ID string `json:"id"`
Bookmarks string `json:"bookmarks"`
Version string `json:"version"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"lastUpdated"`
LastAccessed time.Time `json:"lastAccessed"`
}
type ErrorPayload struct {
Code string `json:"code"`
Message string `json:"message"`

52
syncstore/models.go Normal file
View File

@ -0,0 +1,52 @@
package syncstore
import "time"
type Blob struct {
ID string `json:"id"`
Bookmarks string `json:"bookmarks"`
Version string `json:"version"`
Created time.Time `json:"created"`
LastUpdated time.Time `json:"lastUpdated"`
LastAccessed time.Time `json:"lastAccessed"`
}
type CreateResp struct {
ID string `json:"id"`
LastUpdated time.Time `json:"lastUpdated"`
Version string `json:"version"`
}
type GetResp struct {
Bookmarks string `json:"bookmarks"`
LastUpdated time.Time `json:"lastUpdated"`
Version string `json:"version"`
}
type ServiceInfoResp struct {
Version string `json:"version"`
Message string `json:"message"`
MaxSyncSize int `json:"maxSyncSize"`
Status int `json:"status"`
}
type CreateReq struct {
Version string `json:"version"`
}
type UpdateReq struct {
Bookmarks string `json:"bookmarks"`
LastUpdated time.Time `json:"lastUpdated"`
}
type UpdateResp struct {
LastUpdated time.Time `json:"lastUpdated"`
}
type LastUpdatedResp struct {
LastUpdated time.Time `json:"lastUpdated"`
}
type GetSyncVerResp struct {
Version string `json:"version"`
}

View File

@ -23,18 +23,6 @@ func NewStore(drv StoreDriver) Store {
return Store{drv: drv}
}
type CreateResp struct {
ID string `json:"id"`
LastUpdated time.Time `json:"lastUpdated"`
Version string `json:"version"`
}
type GetResp struct {
Bookmarks string `json:"bookmarks"`
LastUpdated time.Time `json:"lastUpdated"`
Version string `json:"version"`
}
func (store *Store) Create(version string) (*CreateResp, error) {
var sid string
store.lock.Lock()