Skip to content

Commit

Permalink
Merge pull request #76 from nextmn/useragent
Browse files Browse the repository at this point in the history
Add user agent
  • Loading branch information
louisroyer authored Sep 4, 2024
2 parents 8bcf754 + 9360683 commit 1fb3b60
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions internal/tasks/controller-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (
"github.com/nextmn/srv6/internal/ctrl"
)

const UserAgent = "go-github-nextmn-srv6"

// ControllerRegistry registers and unregisters into controller
type ControllerRegistryTask struct {
WithName
WithState
ControllerRegistry *ctrl.ControllerRegistry
SetupRegistry app_api.Registry
httpClient http.Client
}

// Create a new ControllerRegistry
Expand All @@ -37,6 +40,7 @@ func NewControllerRegistryTask(name string, remoteControlURI string, backbone ne
Resource: "",
},
SetupRegistry: setup_registry,
httpClient: http.Client{},
}
}

Expand All @@ -56,8 +60,14 @@ func (t *ControllerRegistryTask) RunInit(ctx context.Context) error {
if err != nil {
return err
}
// TODO: retry on timeout failure
resp, err := http.Post(t.ControllerRegistry.RemoteControlURI+"/routers", "application/json", bytes.NewBuffer(json_data))
// TODO: retry on timeout failure (use a new ctx)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI+"/routers", bytes.NewBuffer(json_data))
if err != nil {
return err
}
req.Header.Add("User-Agent", UserAgent)
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp, err := t.httpClient.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,12 +99,13 @@ func (t *ControllerRegistryTask) RunExit() error {
t.state = false
return nil
}
req, err := http.NewRequest("DELETE", t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
// no context since Background Context is already Done
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
req.Header.Add("User-Agent", UserAgent)
resp, err := t.httpClient.Do(req)
if err != nil {
return err
}
Expand Down

0 comments on commit 1fb3b60

Please sign in to comment.