Skip to content

Instantly share code, notes, and snippets.

@datengaertnerei
Last active November 5, 2021 12:47
Show Gist options
  • Save datengaertnerei/680a1244439d6dfee9a51dd35430cf5d to your computer and use it in GitHub Desktop.
Save datengaertnerei/680a1244439d6dfee9a51dd35430cf5d to your computer and use it in GitHub Desktop.
Simple Go client for test data service
package main
import (
"crypto/tls"
"encoding/csv"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
)
type person struct {
Address struct {
AddressCountry string `json:"addressCountry"`
AddressLocality string `json:"addressLocality"`
HouseNumber string `json:"houseNumber"`
PostalCode string `json:"postalCode"`
StreetAddress string `json:"streetAddress"`
} `json:"address"`
BirthDate string `json:"birthDate"`
BirthName string `json:"birthName"`
Comment string `json:"comment"`
Email string `json:"email"`
Eyecolor string `json:"eyecolor"`
FamilyName string `json:"familyName"`
Gender string `json:"gender"`
GivenName string `json:"givenName"`
Height int `json:"height"`
}
type account struct {
Bank struct {
BankCode string `json:"bankCode"`
Bic string `json:"bic"`
City string `json:"city"`
Desc string `json:"desc"`
} `json:"bank"`
Comment string `json:"comment"`
Iban string `json:"iban"`
}
type phone struct {
Comment string `json:"comment"`
PhoneNumber string `json:"phoneNumber"`
}
type creditcard struct {
Number string `json:"number"`
Type string `json:"type"`
Cvc string `json:"cvc"`
Expiry string `json:"expiry"`
}
var user, pwd string
var client *http.Client
func main() {
// use this for self signed TLS certificate
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
// for Basic Auth
user = "admin"
pwd = "admin"
client = &http.Client{}
w := csv.NewWriter(os.Stdout)
for i := 0; i < 1000; i++ {
record := getRecord()
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
func getRecord() []string {
body := sendRequest("http://localhost/api/v1/person")
pd := person{}
json.Unmarshal(body, &pd)
var record []string = make([]string, 17)
record[0] = pd.FamilyName
record[1] = pd.GivenName
record[2] = pd.BirthDate
record[3] = pd.Email
record[4] = pd.Address.PostalCode
record[5] = pd.Address.AddressLocality
record[6] = pd.Address.StreetAddress
record[7] = pd.Address.HouseNumber
body = sendRequest("http://localhost/api/v1/account")
bd := account{}
json.Unmarshal(body, &bd)
record[8] = bd.Bank.Desc
record[9] = bd.Bank.Bic
record[10] = bd.Iban
body = sendRequest("http://localhost/api/v1/mobile")
md := phone{}
json.Unmarshal(body, &md)
record[11] = md.PhoneNumber
body = sendRequest("http://localhost/api/v1/landline")
json.Unmarshal(body, &md)
record[12] = md.PhoneNumber
body = sendRequest("http://localhost/api/v1/creditcard")
cc := creditcard{}
json.Unmarshal(body, &cc)
record[13] = cc.Type
record[14] = cc.Number
record[15] = cc.Expiry
record[16] = cc.Cvc
return record
}
func sendRequest(url string) []byte {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Panicln(err)
}
req.SetBasicAuth(user, pwd)
resp, err := client.Do(req)
if err != nil {
log.Panicln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Panicln(err)
}
return body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment