func (client *Client) PairClient(paylo map[string]string) (tok Token, err error) { paylo["id"] = client.ClientId sin := ku.GenerateSinFromPem(client.Pem) client.ClientId = sin url := client.ApiUri + "/tokens" htclient := setHttpClient(client) payload, _ := json.Marshal(paylo) req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload)) req.Header.Add("content-type", "application/json") req.Header.Add("accept", "application/json") req.Header.Add("X-accept-version", "2.0.0") response, _ := htclient.Do(req) defer response.Body.Close() contents, _ := ioutil.ReadAll(response.Body) var jsonContents map[string]interface{} json.Unmarshal(contents, &jsonContents) if response.StatusCode/100 != 2 { err = processErrorMessage(response, jsonContents) } else { tok, err = processToken(response, jsonContents) err = nil } return tok, err }
"net/http" "os" "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "strings" ) var _ = Describe("ClientPair", func() { It("pairs with the server with a pairing code", func() { time.Sleep(3 * time.Second) pm := os.ExpandEnv("$BITPAYPEM") pm = strings.Replace(pm, "\\n", "\n", -1) apiuri := os.ExpandEnv("$BITPAYAPI") webClient := Client{ApiUri: apiuri, Insecure: true, Pem: pm, ClientId: ku.GenerateSinFromPem(pm)} mertok, _ := webClient.GetToken("merchant") params := make(map[string]string) params["token"] = mertok params["facade"] = "pos" params["id"] = webClient.ClientId res, err := webClient.Post("tokens", params) defer res.Body.Close() contents, _ := ioutil.ReadAll(res.Body) var jsonContents map[string]interface{} json.Unmarshal(contents, &jsonContents) var tok Token if res.StatusCode/100 != 2 { Expect(res.StatusCode).To(Equal(200), string(contents)+"token: "+mertok) } else { tok, err = processToken(res, jsonContents)