Commit d1c39e3

Richard Luby <richluby@gmail.com>
2016-11-02 13:09:00
client pulls categories from server
client receives list of all available categories from server. client displays category of question next to question itself
1 parent c811596
Changed files (2)
client.go
@@ -92,5 +92,6 @@ func ExecuteClient() {
 		clientConfig = loadClientConfiguration(*filePath)
 	}
 	log.Printf("Configuration: %+v", clientConfig)
+
 	initUserSession()
 }
command.go
@@ -97,7 +97,7 @@ func runTest(clientTest *ClientTest) {
 	reader := bufio.NewReader(os.Stdin)
 	numCorrect := 0
 	for i, record := range clientTest.Records {
-		fmt.Printf("%d) %s\n", i+1, record.Question)
+		fmt.Printf("%d) [%s] %s\n", i+1, categoryKeys[record.Category], record.Question)
 		input, _ := reader.ReadString('\n')
 		input = strings.TrimSpace(input)
 		if strings.Compare(input, record.Answer) == 0 {
@@ -150,6 +150,25 @@ func getRecordFromServer(client *http.Client, config CLIENT_CONFIG, numQuestions
 	return recordArray, err
 }
 
+// getCategoriesFromServer requests a new list of the available
+// categories from the server. functionality provided for
+// command integration as well
+func getCategoriesFromServer(args []string) error {
+	client := &http.Client{}
+	resp, err := client.Get(clientConfig.SERVER_URL + API_ROOT + "/questions/categories")
+	if err != nil {
+		return err
+	}
+	defer resp.Body.Close()
+	data, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return err
+	}
+	err = json.Unmarshal(data, &categoryKeys)
+	log.Printf("Pulled %d categories from server.", len(categoryKeys))
+	return nil
+}
+
 // executeTest runs a user through a test
 // from retrieving the records to returning the answers
 func executeTest(args []string) error {
@@ -168,6 +187,9 @@ func executeTest(args []string) error {
 		return fmt.Errorf("Error while requesting test from server: %+v", err)
 	}
 	clientTest.Username = clientConfig.USER
+	if err = getCategoriesFromServer(nil); err != nil {
+		return fmt.Errorf("Error while pulling server categories: %+v", err)
+	}
 	runTest(&clientTest)
 	fmt.Printf("You scored: %.2f%%.\n", clientTest.Score)
 	return postRecordsToServer(client, &clientTest)