Repository: exercism/cli
Pull request #528 (2018-05-11 07:20:26)

Fix the base URL for the v2 API
We will need to update this before we cut the final release of the new CLI
for launch, since the domain will move from v2.exercism.io to exercism.io.


Patch:
diff --git a/api/client.go b/api/client.go
index eec1af3..d52dea2 100644
--- a/api/client.go
+++ b/api/client.go
@@ -1,6 +1,7 @@
 package api
 
 import (
+	"bytes"
 	"encoding/json"
 	"fmt"
 	"io"
@@ -72,13 +73,13 @@ func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request,
 
 // Do performs an http.Request and optionally parses the response body into the given interface.
 func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
-	debug.DumpRequest(req)
+	debug.Println("Request", req.Method, req.URL)
 
 	res, err := c.Client.Do(req)
 	if err != nil {
 		return nil, err
 	}
-	debug.DumpResponse(res)
+	debug.Printf("Response StatusCode=%d\n", res.StatusCode)
 
 	switch res.StatusCode {
 	case http.StatusNoContent:
@@ -90,7 +91,11 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
 		if v != nil {
 			defer res.Body.Close()
 
-			if err := json.NewDecoder(res.Body).Decode(v); err != nil {
+			var bodyCopy bytes.Buffer
+			body := io.TeeReader(res.Body, &bodyCopy)
+			debug.Printf("Response Body\n%s\n\n", bodyCopy.String())
+
+			if err := json.NewDecoder(body).Decode(v); err != nil {
 				return nil, fmt.Errorf("unable to parse API response - %s", err)
 			}
 		}
diff --git a/config/api_config.go b/config/api_config.go
index 2dadab4..cf70435 100644
--- a/config/api_config.go
+++ b/config/api_config.go
@@ -8,7 +8,7 @@ import (
 )
 
 var (
-	defaultBaseURL   = "https://api.exercism.io/v1"
+	defaultBaseURL   = "https://v2.exercism.io/api/v1"
 	defaultEndpoints = map[string]string{
 		"download":      "/solutions/%s",
 		"submit":        "/solutions/%s",
diff --git a/debug/debug.go b/debug/debug.go
index 1a0eb41..c95d171 100644
--- a/debug/debug.go
+++ b/debug/debug.go
@@ -1,13 +1,8 @@
 package debug
 
 import (
-	"bytes"
 	"fmt"
 	"io"
-	"io/ioutil"
-	"log"
-	"net/http"
-	"net/http/httputil"
 	"os"
 )
 
@@ -30,45 +25,3 @@ func Printf(format string, args ...interface{}) {
 		fmt.Fprintf(output, format, args...)
 	}
 }
-
-func DumpRequest(req *http.Request) {
-	if !Verbose {
-		return
-	}
-
-	var bodyCopy bytes.Buffer
-	body := io.TeeReader(req.Body, &bodyCopy)
-	req.Body = ioutil.NopCloser(body)
-
-	dump, err := httputil.DumpRequest(req, req.ContentLength > 0)
-	if err != nil {
-		log.Fatal(err)
-	}
-
-	Println("\n========================= BEGIN DumpRequest =========================")
-	Println(string(dump))
-	Println("========================= END DumpRequest =========================\n")
-
-	req.Body = ioutil.NopCloser(&bodyCopy)
-}
-
-func DumpResponse(res *http.Response) {
-	if !Verbose {
-		return
-	}
-
-	var bodyCopy bytes.Buffer
-	body := io.TeeReader(res.Body, &bodyCopy)
-	res.Body = ioutil.NopCloser(body)
-
-	dump, err := httputil.DumpResponse(res, res.ContentLength > 0)
-	if err != nil {
-		log.Fatal(err)
-	}
-
-	Println("\n========================= BEGIN DumpResponse =========================")
-	Println(string(dump))
-	Println("========================= END DumpResponse =========================\n")
-
-	res.Body = ioutil.NopCloser(&bodyCopy)
-}


Test patch:
diff --git a/cmd/configure_test.go b/cmd/configure_test.go
index c416889..46bb326 100644
--- a/cmd/configure_test.go
+++ b/cmd/configure_test.go
@@ -42,7 +42,7 @@ func TestConfigure(t *testing.T) {
 			desc:           "It gets the default API base URL.",
 			args:           []string{"fakeapp", "configure"},
 			existingAPICfg: &config.APIConfig{},
-			expectedAPICfg: &config.APIConfig{BaseURL: "https://api.exercism.io/v1"},
+			expectedAPICfg: &config.APIConfig{BaseURL: "https://v2.exercism.io/api/v1"},
 		},
 	}
 
diff --git a/config/api_config_test.go b/config/api_config_test.go
index 55055bf..a27c2e2 100644
--- a/config/api_config_test.go
+++ b/config/api_config_test.go
@@ -42,7 +42,7 @@ func TestAPIConfigSetDefaults(t *testing.T) {
 	// All defaults.
 	cfg := &APIConfig{}
 	cfg.SetDefaults()
-	assert.Equal(t, "https://api.exercism.io/v1", cfg.BaseURL)
+	assert.Equal(t, "https://v2.exercism.io/api/v1", cfg.BaseURL)
 	assert.Equal(t, "/solutions/%s", cfg.Endpoints["download"])
 	assert.Equal(t, "/solutions/%s", cfg.Endpoints["submit"])
 
@@ -62,7 +62,7 @@ func TestAPIConfigSetDefaults(t *testing.T) {
 		},
 	}
 	cfg.SetDefaults()
-	assert.Equal(t, "https://api.exercism.io/v1", cfg.BaseURL)
+	assert.Equal(t, "https://v2.exercism.io/api/v1", cfg.BaseURL)
 	assert.Equal(t, "/download/%d", cfg.Endpoints["download"])
 	assert.Equal(t, "/solutions/%s", cfg.Endpoints["submit"])
 }