Skip to content
Snippets Groups Projects
Commit 43ac9c83 authored by Nils Petter Skålerud's avatar Nils Petter Skålerud
Browse files

Nearly done with deletion of webhook.

parent 2b3639df
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ import (
func main() {
serverState := utils.ServerState{}
serverState := utils.MakeServerState()
NewRequest, err := http.NewRequest(http.MethodGet, utils.REST_COUNTRIES_URL, nil)
if err != nil {
......@@ -100,7 +100,7 @@ func main() {
http.HandleFunc(
utils.NOTIFICATIONS_PATH,
func(w http.ResponseWriter, r *http.Request) {
handlers.WebhookRegistrationHandler(&serverState, w, r)
handlers.NotificationHandler(&serverState, w, r)
})
log.Println("Starting server on port " + port + " ...")
......
......@@ -4,19 +4,30 @@ import (
"Assignment02/utils"
"encoding/json"
"net/http"
"strconv"
)
type WebhookRegistrationSource struct {
func NotificationHandler(serverState *utils.ServerState, w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// Make sure the URL doesn't have anything extra
webhookRegistrationHandler(serverState, w, r)
} else if r.Method == http.MethodDelete {
webhookDeletion(serverState, w, r)
}
}
type WebhookRegistrationBody struct {
Url string `json:"url"`
CountryCode string `json:"country"`
Calls int `json:"calls"`
}
type WebhookRegistrationOutput struct {
Webhook_id int `json:"webhook_id"`
WebhookId int `json:"webhook_id"`
}
func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseWriter, r *http.Request) {
func webhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
return
}
......@@ -25,7 +36,7 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
return
}
var command WebhookRegistrationSource
var command WebhookRegistrationBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&command)
if err != nil {
......@@ -36,11 +47,10 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
Url: command.Url,
Event: command.CountryCode,
}
registrationId := serverState.InsertWebhook(registration)
output := WebhookRegistrationOutput{
Webhook_id: registrationId,
WebhookId: registrationId,
}
encoder := json.NewEncoder(w)
err = encoder.Encode(output)
......@@ -49,3 +59,27 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
}
}
func webhookDeletion(serverState *utils.ServerState, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
return
}
// Extract the ID from the path
remainingPath := r.URL.Path[len(utils.NOTIFICATIONS_PATH):]
if remainingPath == "" {
return
}
// Interpret it as an int
id, err := strconv.Atoi(remainingPath)
if err != nil {
return
}
deletionSuccess := serverState.DeleteWebhook(id)
if !deletionSuccess {
w.WriteHeader(http.StatusBadRequest)
return
}
}
......@@ -6,8 +6,14 @@ type ServerState struct {
startTime time.Time
_useMocking bool
webhook_id_tracker int
webHooks []WebhookRegistration
webhookIdTracker int
webHooks map[int]WebhookRegistration
}
func MakeServerState() ServerState {
out := ServerState{}
out.webHooks = make(map[int]WebhookRegistration)
return out
}
func (state ServerState) UseMocking() bool {
......@@ -19,12 +25,16 @@ func (state ServerState) UptimeInSeconds() float64 {
}
func (state *ServerState) InsertWebhook(registration WebhookRegistration) int {
state.webHooks = append(state.webHooks, registration)
output := state.webhook_id_tracker
state.webhook_id_tracker++
output := state.webhookIdTracker
state.webHooks[output] = registration
state.webhookIdTracker++
return output
}
func (state *ServerState) DeleteWebhook(id int) bool {
return false
}
type CountryItemName struct {
Common string `json:"common"`
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment