Skip to content

Commit 7a5d00f

Browse files
committed
Add convert for single provider with get route, Update readme
1 parent 8465c29 commit 7a5d00f

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ and optionally cache the results.
2424
<img height="64" src="https://image.ibb.co/hvWT2U/go_swap_server_heroku.png" alt="heroku test instance @ https://go-swap-server.herokuapp.com">
2525
</a>
2626

27+
##### /GET Examples:
28+
- [GET /convert?from=USD&to=AED&amount=2&exchanger=yahoo](https://go-swap-server.herokuapp.com/convert?from=USD&to=AED&amount=100&exchanger=yahoo)
29+
- [GET /convert?from=EUR&to=GPB&amount=1&exchanger=google](https://go-swap-server.herokuapp.com/convert?from=EUR&to=GPB&amount=100&exchanger=google)
30+
- [GET /convert?from=EUR&to=GPB&amount=1&exchanger=themoneyconverter](https://go-swap-server.herokuapp.com/convert?from=EUR&to=GPB&amount=100&exchanger=themoneyconverter)
31+
32+
##### /POST Examples:
33+
```bash
34+
35+
```
2736

2837
## QuickStart
2938

@@ -97,13 +106,14 @@ The documentation for the current branch can be found [here](#documentation).
97106
- [x] herokuapp demo
98107
- [x] swagger ui
99108
- [x] examples
109+
- [x] code coverage
110+
- [x] get routes for single provider
100111
- [ ] increase tests
101112
- [ ] goreleaser
102113
- [ ] verbose logging
103114
- [ ] cli convert google GET without payload to be used in binary image
104115
- [ ] cli convert yahoo GET without payload
105116
- [ ] godoc
106-
- [ ] code coverage
107117
- [ ] static bundle public folder `./cmd/server/public`
108118
- [ ] v 1.0.0 release ( docker / binary github / homebrew mac )
109119
- [ ] Benchmark & Performance optimization ` memory leak`

cmd/server/convert.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"bytes"
45
"crypto/md5"
56
"encoding/json"
67
"fmt"
78
ex "github.com/me-io/go-swap/pkg/exchanger"
89
"github.com/me-io/go-swap/pkg/swap"
10+
"io/ioutil"
911
"math"
1012
"net/http"
1113
"time"
@@ -25,6 +27,46 @@ func (c convertReqObj) Hash() string {
2527
}
2628

2729
var Convert = func(w http.ResponseWriter, r *http.Request) {
30+
if r.Method == "POST" {
31+
ConvertPost(w, r)
32+
}
33+
if r.Method == "GET" {
34+
ConvertGet(w, r)
35+
}
36+
}
37+
38+
var ConvertGet = func(w http.ResponseWriter, r *http.Request) {
39+
40+
query := r.URL.Query()
41+
apiKey := query.Get("apiKey")
42+
exchanger := query.Get("exchanger")
43+
amount := query.Get("amount")
44+
from := query.Get("from")
45+
to := query.Get("to")
46+
cacheTime := query.Get("cacheTime")
47+
48+
payload := fmt.Sprintf(`{
49+
"amount": %s,
50+
"exchanger": [
51+
{
52+
"name": "%s",
53+
"apiKey": "%s"
54+
}
55+
],
56+
"from": "%s",
57+
"to": "%s",
58+
"cacheTime":"%s"
59+
}`, amount, exchanger, apiKey, from, to, cacheTime)
60+
61+
fmt.Println(payload)
62+
bytePayload := []byte(payload)
63+
bytePayloadReader := bytes.NewReader(bytePayload)
64+
65+
r.Body = ioutil.NopCloser(bytePayloadReader)
66+
ConvertPost(w, r)
67+
}
68+
69+
var ConvertPost = func(w http.ResponseWriter, r *http.Request) {
2870

2971
convertReq := &convertReqObj{}
3072

@@ -92,7 +134,7 @@ var Convert = func(w http.ResponseWriter, r *http.Request) {
92134
convertRes.From = convertReq.From
93135
convertRes.To = convertReq.To
94136
convertRes.ExchangeValue = rate.GetValue()
95-
convertRes.DateTime = rate.GetDateTime()
137+
convertRes.RateDateTime = rate.GetDateTime()
96138
convertRes.ExchangerName = rate.GetExchangerName()
97139
convertRes.RateFromCache = false
98140

cmd/server/convert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestConvertObj_Convert(t *testing.T) {
109109
r := &http.Request{}
110110
r.Body = ioutil.NopCloser(bytePayloadReader)
111111

112-
Convert(w, r)
112+
ConvertPost(w, r)
113113
assert.Contains(t, testResponse, expectedName[k])
114114
assert.Contains(t, testResponse, expectedRateCache[k])
115115
}

cmd/server/public/swagger/swagger.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ definitions:
125125
type: "number"
126126
format: "float"
127127
example: 27.5814
128-
ConvertedText:
128+
convertedText:
129129
type: "string"
130130
example: "1 USD is worth 3.675 AED"
131-
dateTime:
132-
type: "string"
133-
example: "2018-09-25T12:17:17Z"
134131
exchangerName:
135132
type: "string"
136133
example: "google"
134+
rateDateTime:
135+
type: "string"
136+
example: "2018-09-25T12:17:17Z"
137137
rateFromCache:
138138
type: "boolean"
139139
example: false

cmd/server/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ type convertReqObj struct {
2323
type convertResObj struct {
2424
From string `json:"from"`
2525
To string `json:"to"`
26-
OriginalAmount float64 `json:"originalAmount"`
26+
ExchangerName string `json:"exchangerName"`
2727
ExchangeValue float64 `json:"exchangeValue"`
28+
OriginalAmount float64 `json:"originalAmount"`
2829
ConvertedAmount float64 `json:"convertedAmount"`
2930
ConvertedText string `json:"convertedText"`
30-
DateTime string `json:"dateTime"`
31-
ExchangerName string `json:"exchangerName"`
31+
RateDateTime string `json:"rateDateTime"`
3232
RateFromCache bool `json:"rateFromCache"`
3333
}
3434

0 commit comments

Comments
 (0)