Add checks for client initialization
authorMikael Berthe <mikael@lilotux.net>
Sun, 16 Apr 2017 13:37:37 +0200
changeset 128 a5a00fad7a32
parent 127 96a7f2432d27
child 129 0d4544ce7e5e
Add checks for client initialization
api.go
gondole_test.go
login.go
media.go
streams.go
--- a/api.go	Sun Apr 16 02:12:38 2017 +0200
+++ b/api.go	Sun Apr 16 13:37:37 2017 +0200
@@ -69,7 +69,13 @@
 }
 
 // prepareRequest inserts all pre-defined stuff
-func (g *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (req rest.Request) {
+func (g *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (rest.Request, error) {
+	var req rest.Request
+
+	if g == nil {
+		return req, fmt.Errorf("use of uninitialized gondole client")
+	}
+
 	endPoint := g.APIBase + "/" + target
 
 	// Request headers
@@ -85,13 +91,20 @@
 		Method:      method,
 		QueryParams: params,
 	}
-	return
+	return req, nil
 }
 
 // apiCall makes a call to the Mastodon API server
 func (g *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, data interface{}) error {
+	if g == nil {
+		return fmt.Errorf("use of uninitialized gondole client")
+	}
+
 	// Prepare query
-	req := g.prepareRequest(endPoint, method, params)
+	req, err := g.prepareRequest(endPoint, method, params)
+	if err != nil {
+		return err
+	}
 
 	// Make API call
 	r, err := restAPI(req)
--- a/gondole_test.go	Sun Apr 16 02:12:38 2017 +0200
+++ b/gondole_test.go	Sun Apr 16 13:37:37 2017 +0200
@@ -15,6 +15,7 @@
 		APIBase: "http://example.com",
 	}
 
-	req := g.prepareRequest("bar", rest.Get, nil)
+	req, err := g.prepareRequest("bar", rest.Get, nil)
+	assert.NoError(t, err, "no error")
 	assert.NotNil(t, req.Headers, "not nil")
 }
--- a/login.go	Sun Apr 16 02:12:38 2017 +0200
+++ b/login.go	Sun Apr 16 13:37:37 2017 +0200
@@ -18,6 +18,10 @@
 
 // LoginBasic does basic user authentication
 func (g *Client) LoginBasic(username, password string, scopes []string) error {
+	if g == nil {
+		return fmt.Errorf("use of uninitialized gondole client")
+	}
+
 	if username == "" {
 		return fmt.Errorf("missing username")
 	}
--- a/media.go	Sun Apr 16 02:12:38 2017 +0200
+++ b/media.go	Sun Apr 16 13:37:37 2017 +0200
@@ -37,27 +37,13 @@
 
 	w.Close()
 
-	req := g.prepareRequest("media", rest.Post, nil)
+	req, err := g.prepareRequest("media", rest.Post, nil)
+	if err != nil {
+		return nil, fmt.Errorf("media prepareRequest failed: %s", err.Error())
+	}
 	req.Headers["Content-Type"] = w.FormDataContentType()
 	req.Body = b.Bytes()
 
-	/*
-		reqObj, err := rest.BuildRequestObject(req)
-		if err != nil {
-			return nil, fmt.Errorf("cannot build stream request: %s", err.Error())
-		}
-
-		resp, err := rest.MakeRequest(reqObj)
-		if err != nil {
-			return nil, fmt.Errorf("cannot open stream: %s", err.Error())
-		}
-		if resp.StatusCode != 200 {
-			resp.Body.Close()
-			return nil, errors.New(resp.Status)
-		}
-		return resp, nil
-	*/
-
 	// Make API call
 	r, err := restAPI(req)
 	if err != nil {
--- a/streams.go	Sun Apr 16 02:12:38 2017 +0200
+++ b/streams.go	Sun Apr 16 13:37:37 2017 +0200
@@ -41,7 +41,11 @@
 		return nil, ErrInvalidParameter
 	}
 
-	req := g.prepareRequest("streaming/"+streamName, rest.Get, params)
+	req, err := g.prepareRequest("streaming/"+streamName, rest.Get, params)
+	if err != nil {
+		return nil, fmt.Errorf("cannot build stream request: %s", err.Error())
+	}
+
 	reqObj, err := rest.BuildRequestObject(req)
 	if err != nil {
 		return nil, fmt.Errorf("cannot build stream request: %s", err.Error())
@@ -175,6 +179,10 @@
 // The 'doneCh' channel is closed if the connection is closed by the server.
 // Please note that this method launches a goroutine to listen to the events.
 func (g *Client) StreamListener(name, hashTag string, events chan<- StreamEvent, stopCh <-chan bool, doneCh chan<- bool) error {
+	if g == nil {
+		return fmt.Errorf("use of uninitialized gondole client")
+	}
+
 	resp, err := g.openStream(name, hashTag)
 	if err != nil {
 		return err