Skip to content

Commit

Permalink
return error on get entries
Browse files Browse the repository at this point in the history
supports fixing #10
  • Loading branch information
Strubbl committed Feb 22, 2018
1 parent df0327b commit 1b0b8b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 11 additions & 12 deletions token.go
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)

Expand All @@ -23,7 +22,8 @@ type Token struct {

// getToken will use the credentials set in the configuration to
// request an access token from the wallabag API
func getToken() Token {
func getToken() (Token, error) {
var token Token
tokenURL := Config.WallabagURL + "/oauth/v2/token"
resp, err := http.PostForm(tokenURL,
url.Values{"grant_type": {"password"},
Expand All @@ -33,28 +33,27 @@ func getToken() Token {
"password": {Config.UserPassword},
})
if err != nil {
fmt.Fprintf(os.Stderr, "getToken: getting token failed tokenURL=%s, err=%v\n", tokenURL, err)
return token, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "getToken: bad response from server: %v\n", resp.StatusCode)
return token, fmt.Errorf("getToken: bad response from server: %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "getToken: error while ioutil.ReadAll %v\n", err)
return token, err
}
//log.Printf("GetToken: body=%v\n", string(body))
var token Token
if err := json.Unmarshal(body, &token); err != nil {
fmt.Fprintf(os.Stderr, "getToken: getting token failed %s: %v\n", tokenURL, err)
}
return token
err = json.Unmarshal(body, &token)
return token, err
}

func checkForToken() {
func checkForToken() error {
var err error
if token.TokenType == "" || token.AccessToken == "" {
token = getToken()
token, err = getToken()
}
return err
}

// GetAuthTokenHeader will make sure there's a working token and
Expand Down
5 changes: 4 additions & 1 deletion token_test.go
Expand Up @@ -19,7 +19,10 @@ func TestGetToken(t *testing.T) {
fmt.Fprintln(w, `{"access_token":"294hf92ufurjfgoiqjfioj4","refresh_token": "ZGE5MDg3ZTNjNmNkYTY0ZWZh","expires_in":3600,"scope": "null", "token_type": "bearer"}`)
})

token := getToken()
token, err := getToken()
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
expectedToken := Token{"294hf92ufurjfgoiqjfioj4", 3600, "bearer", "null", "ZGE5MDg3ZTNjNmNkYTY0ZWZh"}
if token != expectedToken {
t.Errorf("TestGetToken(): expected %v, got %v", expectedToken, token)
Expand Down

0 comments on commit 1b0b8b8

Please sign in to comment.