aboutsummaryrefslogtreecommitdiffstats
path: root/gosrc/requests.go
blob: 0a939c0b2ee867e5f0244295292d780f4789d3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//   SMITH            (  //       /
//   requests          ( )/       /
//   by salade         )(/        /
//  ________________  ( /)        /
// ()__)____________)))))   :^}   /

package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
)

// #include "../csrc/smith.h"
// #cgo LDFLAGS: -lsmith -L../
import "C"

// send request to mirror and return response
// @param body      ?qwe=asd&foo=bar
// @param signature duh
// @param endpoint  /sapi/v1/capital/config/qwe
// @param config    duhh
func send_request(body string, signature string, endpoint string, config Config) (*http.Response, error) {
	// new client to allow defering of requests
	client := &http.Client{}
	url := "https://" + config.Mirror + "/" + endpoint + "?" + body + "&signature=" + signature

	// add some ifs here
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Fatal("error: creating http request ", err)
	}
	req.Header.Add("X-MBX-APIKEY", config.Public_key)

	// finished bakiong request. send it
	response, err := client.Do(req)
	if err != nil {
		log.Fatal("error: making http request ", err)
	}

	defer response.Body.Close()
	return response, nil
}

// sign request with given private key
func sign_request(body string, key string) string {
	fmt.Println("signing request : ", body)

	// run pipeline
	out2, err := RunStrings("/usr/bin/echo", "-n", body, "|", "/usr/bin/openssl", "dgst", "-sha256", "-hmac", key)
	if err != nil {
		log.Fatal("error: failed to sign request", err)
	}

	// remove unwanted characters
	tok := strings.Index(out2, "(")
	last := len(out2) - 1
	first := tok + len("(stdin)= ")
	out2 = out2[first:last]
	return out2
}

// create body given choice ex :
// GET /sapi/v1/capital/config/getall ||
// POST /sapi/v1/asset/dust-btc
func make_body() string {
	ret := "timestamp=" + C.GoString(C.get_timestamp()) + "&recvWindow=50000"
	return ret
}