Skip to content

Commit

Permalink
Move fetch/unmarshal to dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
ljagiello committed Oct 10, 2023
1 parent 51a9ba7 commit 7b913a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 16 additions & 0 deletions airgradient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"io"
"net/http"
"time"
Expand Down Expand Up @@ -59,3 +60,18 @@ func fetchMeasures(token string) ([]byte, error) {

return body, nil
}

func getAirGradientMeasures(token string) (airGradientMeasures AirGradientMeasures) {
payload, err := fetchMeasures(token)
if err != nil {
logger.Error("Fetching measures", "error", err)
return
}

err = json.Unmarshal(payload, &airGradientMeasures)
if err != nil {
logger.Error("Parsing JSON payload", "error", err)
return
}
return airGradientMeasures
}
14 changes: 2 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -45,18 +44,9 @@ func launched(app appkit.Application, delegate *appkit.ApplicationDelegate) {
for {
select {
case <-time.After(time.Duration(cfg.Interval) * time.Second):
payload, err := fetchMeasures(cfg.Token)
if err != nil {
logger.Error("Fetching measures", "error", err)
return
}

err = json.Unmarshal(payload, &airGradientMeasures)
if err != nil {
logger.Error("Parsing JSON payload", "error", err)
return
}
airGradientMeasures = getAirGradientMeasures(cfg.Token)
}

if len(airGradientMeasures) == 0 {
logger.Error("No measurements found")
return
Expand Down

0 comments on commit 7b913a6

Please sign in to comment.