Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve API consistency #184

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/postman/shiori.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"response": []
},
{
"name": "/api/tag",
"name": "/api/tags",
"request": {
"method": "PUT",
"header": [
Expand All @@ -112,13 +112,13 @@
"raw": "{\n\t\"id\": 1,\n \"name\": \"renamed_tag_7\"\n}"
},
"url": {
"raw": "{{host}}/api/tag",
"raw": "{{host}}/api/tags",
"host": [
"{{host}}"
],
"path": [
"api",
"tag"
"tags"
]
}
},
Expand Down
7 changes: 2 additions & 5 deletions internal/view/js/page/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default {
return response.json();
})
.then(json => {
this.tags = json;
this.tags = json.tags;
this.loading = false;
})
.catch(err => {
Expand Down Expand Up @@ -766,13 +766,10 @@ export default {
};

this.dialog.loading = true;
fetch(new URL("api/tag", document.baseURI), {
fetch(new URL("api/tags", document.baseURI), {
method: "PUT",
body: JSON.stringify(newData),
headers: { "Content-Type": "application/json" },
}).then(response => {
if (!response.ok) throw response;
return response.json();
}).then(() => {
tag.name = data.newName;

Expand Down
2 changes: 1 addition & 1 deletion internal/view/js/page/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default {
})
.then(json => {
this.loading = false;
this.accounts = json;
this.accounts = json.accounts;
})
.catch(err => {
this.loading = false;
Expand Down
128 changes: 64 additions & 64 deletions internal/webserver/assets-prod.go

Large diffs are not rendered by default.

26 changes: 17 additions & 9 deletions internal/webserver/handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (h *handler) apiLogout(w http.ResponseWriter, r *http.Request, ps httproute
h.SessionCache.Delete(sessionID)
}

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}

// apiGetBookmarks is handler for GET /api/bookmarks
Expand Down Expand Up @@ -225,12 +225,16 @@ func (h *handler) apiGetTags(w http.ResponseWriter, r *http.Request, ps httprout
tags, err := h.DB.GetTags()
checkError(err)

resp := map[string]interface{}{
"tags": tags,
}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&tags)
err = json.NewEncoder(w).Encode(&resp)
checkError(err)
}

// apiRenameTag is handler for PUT /api/tag
// apiRenameTag is handler for PUT /api/tags
func (h *handler) apiRenameTag(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Make sure session still valid
err := h.validateSession(r)
Expand All @@ -245,7 +249,7 @@ func (h *handler) apiRenameTag(w http.ResponseWriter, r *http.Request, ps httpro
err = h.DB.RenameTag(tag.ID, tag.Name)
checkError(err)

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}

// Bookmark is the record for an URL.
Expand Down Expand Up @@ -356,7 +360,7 @@ func (h *handler) apiDeleteBookmark(w http.ResponseWriter, r *http.Request, ps h
os.Remove(archivePath)
}

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}

// apiUpdateBookmark is handler for PUT /api/bookmarks
Expand Down Expand Up @@ -631,8 +635,12 @@ func (h *handler) apiGetAccounts(w http.ResponseWriter, r *http.Request, ps http
accounts, err := h.DB.GetAccounts(database.GetAccountsOptions{})
checkError(err)

resp := map[string]interface{}{
"accounts": accounts,
}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&accounts)
err = json.NewEncoder(w).Encode(&resp)
checkError(err)
}

Expand All @@ -651,7 +659,7 @@ func (h *handler) apiInsertAccount(w http.ResponseWriter, r *http.Request, ps ht
err = h.DB.SaveAccount(account)
checkError(err)

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}

// apiUpdateAccount is handler for PUT /api/accounts
Expand Down Expand Up @@ -699,7 +707,7 @@ func (h *handler) apiUpdateAccount(w http.ResponseWriter, r *http.Request, ps ht
h.UserCache.Delete(request.Username)
}

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}

// apiDeleteAccount is handler for DELETE /api/accounts
Expand Down Expand Up @@ -730,5 +738,5 @@ func (h *handler) apiDeleteAccount(w http.ResponseWriter, r *http.Request, ps ht
}
}

fmt.Fprint(w, 1)
w.WriteHeader(http.StatusNoContent)
}
2 changes: 1 addition & 1 deletion internal/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func ServeApp(cfg Config) error {
router.POST(jp("/api/logout"), withLogging(hdl.apiLogout))
router.GET(jp("/api/bookmarks"), withLogging(hdl.apiGetBookmarks))
router.GET(jp("/api/tags"), withLogging(hdl.apiGetTags))
router.PUT(jp("/api/tag"), withLogging(hdl.apiRenameTag))
router.PUT(jp("/api/tags"), withLogging(hdl.apiRenameTag))
router.POST(jp("/api/bookmarks"), withLogging(hdl.apiInsertBookmark))
router.DELETE(jp("/api/bookmarks"), withLogging(hdl.apiDeleteBookmark))
router.PUT(jp("/api/bookmarks"), withLogging(hdl.apiUpdateBookmark))
Expand Down