Skip to content

Commit

Permalink
Modify httpClient package to use external clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel-Dorta committed Sep 14, 2019
1 parent c916fc4 commit 02fa69d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/fetchImage/fetchImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Miguel-Dorta/surveillance-cameras/internal"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/utils"
"golang.org/x/sys/unix"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand All @@ -17,6 +18,7 @@ var (
destinyDir, fileExtension string
printVersion bool
tomorrow time.Time
httpClient = http.Client{Timeout: time.Second}
)

func init() {
Expand Down Expand Up @@ -68,7 +70,7 @@ MainLoop:
requestTime := time.Now()
updateDestinyDir(requestTime)

if err := utils.GetFileWithLogin(url, user, pass, getNewFilePath(path, requestTime)); err != nil {
if err := utils.GetFileWithLogin(url, user, pass, getNewFilePath(path, requestTime), httpClient); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error downloading image: %s", err)
}
}
Expand Down
19 changes: 14 additions & 5 deletions cmd/fetchVideo/fetchVideo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Miguel-Dorta/surveillance-cameras/pkg/utils"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
Expand All @@ -23,6 +24,7 @@ var (
url, user, pass, camName, destination string
printVersion bool
logErr = log.New(os.Stderr, "", 0)
httpC = http.Client{}
)

func init() {
Expand Down Expand Up @@ -58,8 +60,11 @@ func main() {
}
errFound := false
for _, link := range linkVideos {
pathToSave := getSavingPath(link.Text, camName, destination)
if err = utils.GetFileWithLogin(url + link.HREF, user, pass, pathToSave); err != nil {
pathToSave, err := createAndGetSavingPath(link.Text, camName, destination)
if err != nil {
logErr.Fatalf("cannot create parent directories of file \"%s\": %s", link.Text, err)
}
if err = utils.GetFileWithLogin(url + link.HREF, user, pass, pathToSave, httpC); err != nil {
logErr.Printf("error saving file in path \"%s\": %s", pathToSave, err)
errFound = true
continue
Expand All @@ -70,9 +75,13 @@ func main() {
}
}

func getSavingPath(filename, camName, destination string) string {
func createAndGetSavingPath(filename, camName, destination string) (string, error) {
y, m, d, rest := cameras.GetInfoFromFilenameOWIPCAN45(filename)
return filepath.Join(destination, camName, "20" + y, m, d, rest)
parentDirPath := filepath.Join(destination, camName, "20" + y, m, d)
if err := os.MkdirAll(parentDirPath, 0755); err != nil {
return "", fmt.Errorf("error creating parent directories: %s", err)
}
return filepath.Join(parentDirPath, rest), nil
}

func getAllVideos(url, user, pass string) ([]html.A, error) {
Expand Down Expand Up @@ -116,7 +125,7 @@ func getVideoLinks(url, user, pass string) ([]html.A, error) {
}

func getPage(url, user, pass string) ([]byte, error) {
resp, err := httpClient.GetLogin(url, user, pass)
resp, err := httpClient.GetLogin(url, user, pass, httpC)
if err != nil {
return nil, fmt.Errorf("error getting page from URL \"%s\": %s", url, err)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/httpClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package httpClient
import (
"fmt"
"net/http"
"time"
)

var c = http.Client{Timeout: time.Second}

func GetLogin(url, user, pass string) (*http.Response, error) {
func GetLogin(url, user, pass string, c http.Client) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %s", err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Miguel-Dorta/surveillance-cameras/pkg/httpClient"
"golang.org/x/sys/unix"
"io"
"net/http"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -71,8 +72,8 @@ func Copy(origin, destiny string) error {
return nil
}

func GetFileWithLogin(url, user, pass, destination string) error {
resp, err := httpClient.GetLogin(url, user, pass)
func GetFileWithLogin(url, user, pass, destination string, c http.Client) error {
resp, err := httpClient.GetLogin(url, user, pass, c)
if err != nil {
return fmt.Errorf("error doing http request to URL \"%s\": %s", url, err)
}
Expand Down

0 comments on commit 02fa69d

Please sign in to comment.