Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.1.6
version: v2.12.2
69 changes: 37 additions & 32 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
version: "2"
run:
tests: false
linters:
default: all
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
- dogsled
- dupl
- durationcheck
- errchkjson
- errorlint
- exhaustive
- gocheckcompilerdirectives
- gochecksumtype
- gosec
- gosmopolitan
- loggercheck
- makezero
- musttag
- nilerr
- nilnesserr
- noctx
- protogetter
- reassign
- recvcheck
- rowserrcheck
- spancheck
- sqlclosecheck
- testifylint
- unparam
- whitespace
- wsl
- zerologlint
- wsl_v5
- gomodguard_v2
disable:
- wsl
- gomodguard
- cyclop
- depguard
- err113
- exhaustruct
- forbidigo
- forcetypeassert
- gochecknoglobals
- gochecknoinits
- godot
- godox
- lll
- mnd
- musttag
- nakedret
- nlreturn
- nolintlint
- nonamedreturns
- tagliatelle
- varnamelen
- wrapcheck
- funlen
settings:
wsl_v5:
allow-first-in-block: true
allow-whole-block: true
branch-max-lines: 2
disable:
- err
exclusions:
generated: lax
presets:
Expand All @@ -47,6 +49,9 @@ linters:
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ builds:
- CGO_ENABLED=0
goarch:
- amd64
- arm64
goos:
- linux
ldflags:
Expand Down
57 changes: 37 additions & 20 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,47 @@ import (
"time"
)

type ApiClient struct {
Client *http.Client
Gateway string
Token string
Timeout time.Duration
UseTls bool
const DefaultTimeout = 5

type AuthTokenAnswer struct {
Token string `json:"jwt"`
ExpireAt uint `json:"expireAt"`
}

const DefaultTimeout = 5
type Recipient struct {
To string `json:"to"`
Target string `json:"target"`
}

type Message struct {
Recipients []Recipient `json:"recipients"`
Text string `json:"text"`
Provider string `json:"provider"`
Type string `json:"type"`
}

type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}

func NewApiClient(gateway string) *ApiClient {
return &ApiClient{
type APIClient struct {
Client *http.Client
Gateway string
Token string
Timeout time.Duration
UseTLS bool
}

func NewAPIClient(gateway string) *APIClient {
return &APIClient{
Client: &http.Client{},
Gateway: gateway,
Timeout: DefaultTimeout * time.Second,
}
}

func (ac *ApiClient) Login(username, password string) (err error) {
func (ac *APIClient) Login(username, password string) (err error) {
ac.Token = ""

response, err := ac.DoRequest("signin", Credentials{username, password})
Expand All @@ -57,7 +74,7 @@ func (ac *ApiClient) Login(username, password string) (err error) {
return
}

func (ac *ApiClient) DoLegacyRequest(mode string,
func (ac *APIClient) DoLegacyRequest(mode string,
to string,
text string,
username string,
Expand All @@ -80,17 +97,17 @@ func (ac *ApiClient) DoLegacyRequest(mode string,

schema := "http://"

if ac.UseTls {
if ac.UseTLS {
schema = "https://"
}

myUrl := schema + ac.Gateway + "/api.php" + "?" + params.Encode()
u := schema + ac.Gateway + "/api.php" + "?" + params.Encode()

// Setup Timeout context
ctx, cancel := context.WithTimeout(context.Background(), ac.Timeout)
defer cancel()

req, err := http.NewRequestWithContext(ctx, "GET", myUrl, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)

if err != nil {
return err
Expand All @@ -116,15 +133,15 @@ func (ac *ApiClient) DoLegacyRequest(mode string,

resp.Body.Close()

if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("API request failed with status %d", resp.StatusCode)
return err
}

return nil
}

func (ac *ApiClient) DoRequest(rawUrl string, body interface{}) (respBody []byte, err error) {
func (ac *APIClient) DoRequest(rawURL string, body any) (respBody []byte, err error) {
// Build request body as JSON
var buf bytes.Buffer

Expand All @@ -142,14 +159,14 @@ func (ac *ApiClient) DoRequest(rawUrl string, body interface{}) (respBody []byte

schema := "http://"

if ac.UseTls {
if ac.UseTLS {
schema = "https://"
}

// Build Request
baseUrl := schema + ac.Gateway + "/api/"
b := schema + ac.Gateway + "/api/"

req, err := http.NewRequestWithContext(ctx, "POST", baseUrl+rawUrl, &buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, b+rawURL, &buf)

if err != nil {
return []byte(""), err
Expand Down Expand Up @@ -181,7 +198,7 @@ func (ac *ApiClient) DoRequest(rawUrl string, body interface{}) (respBody []byte

resp.Body.Close()

if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("API request failed with status %d", resp.StatusCode)
return
}
Expand Down
16 changes: 8 additions & 8 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/jarcoal/httpmock"
)

func TestApiClient_Login(t *testing.T) {
func TestAPIClient_Login(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("POST", "http://brevisone.local/api/signin",
httpmock.NewStringResponder(200, `{"jwt":"abc123","expireAt":0}`))

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")

err := ac.Login("admin", "password")
if err != nil {
Expand All @@ -43,7 +43,7 @@ func TestApiClient_LoginTimeout(t *testing.T) {
},
)

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")

ac.Timeout = 1 * time.Second
err := ac.Login("admin", "password")
Expand All @@ -60,7 +60,7 @@ func TestApiClient_LoginErr(t *testing.T) {
httpmock.RegisterResponder("POST", "https://brevisone.local/api/signin",
httpmock.NewStringResponder(401, `{}`))

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")

err := ac.Login("admin", "password")
if err == nil {
Expand All @@ -75,7 +75,7 @@ func TestApiClient_UnmarshalErr(t *testing.T) {
httpmock.RegisterResponder("POST", "https://brevisone.local/api/signin",
httpmock.NewStringResponder(200, `{`))

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")

err := ac.Login("admin", "password")
if err == nil {
Expand All @@ -90,7 +90,7 @@ func TestApiClient_DoRequest(t *testing.T) {
httpmock.RegisterResponder("POST", "http://brevisone.local/api/test",
httpmock.NewStringResponder(200, `{"test":true}`))

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")
ac.Token = "abc1234"

response, err := ac.DoRequest("test", nil)
Expand All @@ -111,7 +111,7 @@ func TestApiClient_DoLegacyRequest(t *testing.T) {
httpmock.RegisterResponder("GET", "http://brevisone.local/api.php?mode=number&password=password&text=text&to=to&username=username",
httpmock.NewStringResponder(200, `{"test":true}`))

ac := NewApiClient("brevisone.local")
ac := NewAPIClient("brevisone.local")
ac.Token = "abc1234"

err := ac.DoLegacyRequest("test", "to", "text", "username", "password")
Expand All @@ -121,7 +121,7 @@ func TestApiClient_DoLegacyRequest(t *testing.T) {
}

func TestApiClient_DoRequestErr(t *testing.T) {
ac := NewApiClient("local")
ac := NewAPIClient("local")

_, err := ac.DoRequest("test", nil)
if err == nil {
Expand Down
18 changes: 0 additions & 18 deletions api_types.go

This file was deleted.

19 changes: 10 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Config struct {
date string
notificationType string
doNotUseTLS bool
useLegacyHttpApi bool
useLegacyAPI bool
}

func (c *Config) BindArguments(fs *pflag.FlagSet) {
Expand All @@ -40,7 +40,7 @@ func (c *Config) BindArguments(fs *pflag.FlagSet) {
fs.BoolVar(&c.insecure, "insecure", false,
"Skip verification of the TLS certificates (is needed for the default self signed certificate, default false)")
fs.BoolVar(&c.doNotUseTLS, "no-tls", false, "Do NOT use TLS to connect to the gateway (default false)")
fs.BoolVar(&c.useLegacyHttpApi, "use-legacy-http-api", false, "Use old HTTP API (required on older firmware versions, default false)")
fs.BoolVar(&c.useLegacyAPI, "use-legacy-http-api", false, "Use old HTTP API (required on older firmware versions, default false)")

// Where to send the message to
fs.StringVarP(&c.target, "target", "T", "", "Target contact, group or phone number (required)")
Expand Down Expand Up @@ -107,16 +107,16 @@ func (c *Config) FormatMessage() string {

if c.serviceName != "" {
// This is a service notification
msg.WriteString(fmt.Sprintf("%s @ %s - %s", c.serviceName, c.hostName, c.checkState))
fmt.Fprintf(&msg, "%s @ %s - %s", c.serviceName, c.hostName, c.checkState)
} else {
msg.WriteString(fmt.Sprintf("%s - %s", c.hostName, c.checkState))
fmt.Fprintf(&msg, "%s - %s", c.hostName, c.checkState)
}

if c.comment != "" {
msg.WriteString(fmt.Sprintf("\r\n\"%s\"", c.comment))
fmt.Fprintf(&msg, "\r\n\"%s\"", c.comment)

if c.author != "" {
msg.WriteString(fmt.Sprintf(` by %s`, c.author))
msg.WriteString(" by " + c.author)
}
}

Expand All @@ -134,7 +134,7 @@ func (c *Config) FormatMessage() string {

func (c *Config) Run() (err error) {
// Setup API client
api := NewApiClient(c.gateway)
api := NewAPIClient(c.gateway)

// Update client to allow insecure when requested
if c.insecure {
Expand All @@ -145,9 +145,10 @@ func (c *Config) Run() (err error) {
}
}

api.UseTls = !c.doNotUseTLS
api.UseTLS = !c.doNotUseTLS

if c.useLegacyHttpApi {
//nolint: nestif
if c.useLegacyAPI {
err := api.DoLegacyRequest(c.targetType,
c.target,
c.FormatMessage(),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NETWAYS/notify-brevisone
go 1.24

require (
github.com/NETWAYS/go-check v0.6.4
github.com/NETWAYS/go-check v1.0.0
github.com/jarcoal/httpmock v1.4.1
github.com/spf13/pflag v1.0.10
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/NETWAYS/go-check v0.6.4 h1:4WETSVNZNEP0Yudcp5xlvxq6RGn920cmUKq4fz/P1GQ=
github.com/NETWAYS/go-check v0.6.4/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s=
github.com/NETWAYS/go-check v1.0.0 h1:YkzTwFfGR+Z+mK3Wsqpnu8wibzsB30im19iPNfCOsMQ=
github.com/NETWAYS/go-check v1.0.0/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jarcoal/httpmock v1.4.1 h1:0Ju+VCFuARfFlhVXFc2HxlcQkfB+Xq12/EotHko+x2A=
Expand Down
Loading