Commit 5c45326

Richard Luby <richluby@gmail.com>
2016-10-26 14:47:57
client requests questions
client can request questions from server and display them
1 parent 35871ec
Changed files (1)
client.go
@@ -1,10 +1,14 @@
 package main
 
 import (
+	"encoding/json"
 	"flag"
 	"github.com/BurntSushi/toml"
+	"io/ioutil"
 	"log"
+	"net/http"
 	"os"
+	"strconv"
 	"strings"
 )
 
@@ -33,6 +37,42 @@ func loadClientConfiguration(cfgFile string) CONFIG {
 	return config
 }
 
+// getRecordFromServer retrieves a record from the server
+func getRecordFromServer(client *http.Client, config CONFIG) (Record, error) {
+	var serverURL string
+	// allows adding configuration for port specific (ie HTTPS) requests
+	switch config.PORT {
+	case 0:
+		serverURL = config.SERVER
+	default:
+		serverURL = "http://" + config.SERVER + ":" + strconv.Itoa(config.PORT)
+	}
+	resp, err := client.Get(serverURL)
+	if err != nil {
+		return Record{}, err
+	}
+	defer resp.Body.Close()
+	var record Record
+	data, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return Record{}, err
+	}
+	err = json.Unmarshal(data, &record)
+	return record, err
+}
+
+// initUserSession starts the interactive prompt for the user
+func initUserSession(config CONFIG) {
+	client := &http.Client{}
+	var record Record
+	var err error
+	record, err = getRecordFromServer(client, config)
+	if err != nil {
+		log.Printf("Error for request: %+v", err)
+	}
+	log.Printf("Record: %+v", record)
+}
+
 // initializes the client and handles the user interaction
 func main() {
 	filePath := flag.String("file", "", "defines a path to the configuration file")