-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e185537
Showing
10 changed files
with
1,434 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Build | ||
artifacts/ | ||
|
||
# Golang | ||
vendor/ | ||
|
||
# IDEs | ||
.idea/ | ||
.vscode/ | ||
|
||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Claudio Candio | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[![GitHub license](https://claudiocandio.github.io/img/license_mit.svg)](https://github.com/claudiocandio/gemini-api/blob/master/LICENSE) | ||
[![Language: Go](https://claudiocandio.github.io/img/language-Go.svg)](https://golang.org/) | ||
[![Donate Bitcoin](https://claudiocandio.github.io/img/donate-bitcoin-orange.svg)](https://claudiocandio.github.io/img/donate-bitcoin.html) | ||
[![Donate Ethereum](https://claudiocandio.github.io/img/donate-etherum-green.svg)](https://claudiocandio.github.io/img/donate-ethereum.html) | ||
|
||
# API wrapper for the Gemini Exchange REST API | ||
|
||
Gemini-api is a wrapper for the Gemini Exchange REST API <https://docs.gemini.com/rest-api/> it can connect to the production site <https://api.gemini.com> or to the Sandbox site <https://api.sandbox.gemini.com> for testing purposes. | ||
|
||
This package is based on the <https://github.com/jsgoyette/gemini>, I have rewritten many things, added some more functionalities, debug & trace logging but I haven't had time to fully test everything, it is working fine for me but use it at your own risk. | ||
|
||
## gemini_cli | ||
|
||
This package is used by the gemini_cli <https://github.com/claudiocandio/gemini_cli> a cli command to facilitate the use of the Gemini Exchange via REST API. | ||
|
||
## Usage Example | ||
|
||
```bash | ||
$ go get github.com/claudiocandio/gemini-api | ||
``` | ||
|
||
Simple example: | ||
|
||
```golang | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/claudiocandio/gemini-api" | ||
) | ||
|
||
func main() { | ||
|
||
api := gemini.New( | ||
false, // if this is false, it will use Gemini Sandox site: <https://api.sandbox.gemini.com> | ||
// if this is true, it will use Gemini Production site: <https://api.gemini.com> | ||
"MyGeminiApiKey", // GEMINI_API_KEY | ||
"MyGeminiApiSecret", // GEMINI_API_SECRET | ||
) | ||
|
||
// check more api methods in private.go & public.go | ||
accountDetail, err := api.AccountDetail() | ||
if err != nil { | ||
fmt.Printf("Error AccountDetail: %s\n", err) | ||
return | ||
} | ||
j, err := json.MarshalIndent(&accountDetail, "", " ") | ||
if err != nil { | ||
fmt.Printf("Error MarshalIndent: %s\n", err) | ||
} | ||
|
||
fmt.Printf("%s", j) | ||
} | ||
``` | ||
|
||
Check more api methods in private.go & public.go | ||
|
||
## Disclaimer | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/claudiocandio/gemini-api" | ||
) | ||
|
||
func main() { | ||
|
||
api := gemini.New( | ||
false, // if this is false, it will use Gemini Sandox site: <https://api.sandbox.gemini.com> | ||
// if this is true, it will use Gemini Production site: <https://api.gemini.com> | ||
"MyGeminiApiKey", // GEMINI_API_KEY | ||
"MyGeminiApiSecret", // GEMINI_API_SECRET | ||
) | ||
|
||
// check more api methods in private.go & public.go | ||
accountDetail, err := api.AccountDetail() | ||
if err != nil { | ||
fmt.Printf("Error AccountDetail: %s\n", err) | ||
return | ||
} | ||
j, err := json.MarshalIndent(&accountDetail, "", " ") | ||
if err != nil { | ||
fmt.Printf("Error MarshalIndent: %s\n", err) | ||
} | ||
|
||
fmt.Printf("%s", j) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package gemini | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"crypto/sha512" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/claudiocandio/gemini-api/logger" | ||
) | ||
|
||
type Api struct { | ||
url string | ||
key string | ||
secret string | ||
} | ||
|
||
// buildHeader handles the conversion of post parameters into headers formatted | ||
// according to Gemini specification. Resulting headers include the API key, | ||
// the payload and the signature. | ||
func (api *Api) buildHeader(req *map[string]interface{}) http.Header { | ||
|
||
reqStr, _ := json.Marshal(req) | ||
payload := base64.StdEncoding.EncodeToString([]byte(reqStr)) | ||
|
||
mac := hmac.New(sha512.New384, []byte(api.secret)) | ||
if _, err := mac.Write([]byte(payload)); err != nil { | ||
panic(err) | ||
} | ||
|
||
signature := hex.EncodeToString(mac.Sum(nil)) | ||
|
||
header := http.Header{} | ||
header.Set("X-GEMINI-APIKEY", api.key) | ||
header.Set("X-GEMINI-PAYLOAD", payload) | ||
header.Set("X-GEMINI-SIGNATURE", signature) | ||
|
||
return header | ||
} | ||
|
||
// request makes the HTTP request to Gemini and handles any returned errors | ||
func (api *Api) request(verb, url string, params map[string]interface{}) ([]byte, error) { | ||
|
||
logger.Debug("func request: http.NewRequest", | ||
fmt.Sprintf("verb:%s", verb), | ||
fmt.Sprintf("url:%s", url), | ||
fmt.Sprintf("params:%v", params), | ||
) | ||
|
||
req, err := http.NewRequest(verb, url, bytes.NewBuffer([]byte{})) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if params != nil { | ||
if verb == "GET" { | ||
q := req.URL.Query() | ||
for key, val := range params { | ||
q.Add(key, val.(string)) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
} else { | ||
req.Header = api.buildHeader(¶ms) | ||
} | ||
} | ||
|
||
// this will also show gemini key and secret, pay attention | ||
logger.Trace("func request: params", | ||
fmt.Sprintf("req:%v", req), | ||
) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
logger.Debug("func request: Http Client response", | ||
fmt.Sprintf("resp:%v", resp), | ||
) | ||
|
||
if resp.StatusCode != 200 { | ||
statusCode := fmt.Sprintf("HTTP Status Code: %d", resp.StatusCode) | ||
if resp.StatusCode >= 300 && resp.StatusCode < 400 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "API entry point has moved, see Location: header. Most likely an http: to https: redirect.") | ||
} else if resp.StatusCode == 400 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "Auction not open or paused, ineligible timing, market not open, or the request was malformed; in the case of a private API request, missing or malformed Gemini private API authentication headers") | ||
} else if resp.StatusCode == 403 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "The API key is missing the role necessary to access this private API endpoint") | ||
} else if resp.StatusCode == 404 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "Unknown API entry point or Order not found") | ||
} else if resp.StatusCode == 406 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "Insufficient Funds") | ||
} else if resp.StatusCode == 429 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "Rate Limiting was applied") | ||
} else if resp.StatusCode == 500 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "The server encountered an error") | ||
} else if resp.StatusCode == 502 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "Technical issues are preventing the request from being satisfied") | ||
} else if resp.StatusCode == 503 { | ||
return nil, fmt.Errorf("%s\n%s", statusCode, "The exchange is down for maintenance") | ||
} | ||
} | ||
|
||
// read response body | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logger.Debug("func request: Http Client body", | ||
fmt.Sprintf("body:%s", body), | ||
) | ||
|
||
return body, nil | ||
} | ||
|
||
func New(live bool, key, secret string) *Api { | ||
var url string | ||
if url = sandbox_URL; live { | ||
url = base_URL | ||
} | ||
|
||
return &Api{url: url, key: key, secret: secret} | ||
} |
Oops, something went wrong.