Skip to content

Commit

Permalink
Add convert for single provider with get route, Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
meabed committed Sep 30, 2018
1 parent 8465c29 commit 7a5d00f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ and optionally cache the results.
<img height="64" src="https://image.ibb.co/hvWT2U/go_swap_server_heroku.png" alt="heroku test instance @ https://go-swap-server.herokuapp.com">
</a>

##### /GET Examples:
- [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)
- [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)
- [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)

##### /POST Examples:
```bash

```

## QuickStart

Expand Down Expand Up @@ -97,13 +106,14 @@ The documentation for the current branch can be found [here](#documentation).
- [x] herokuapp demo
- [x] swagger ui
- [x] examples
- [x] code coverage
- [x] get routes for single provider
- [ ] increase tests
- [ ] goreleaser
- [ ] verbose logging
- [ ] cli convert google GET without payload to be used in binary image
- [ ] cli convert yahoo GET without payload
- [ ] godoc
- [ ] code coverage
- [ ] static bundle public folder `./cmd/server/public`
- [ ] v 1.0.0 release ( docker / binary github / homebrew mac )
- [ ] Benchmark & Performance optimization ` memory leak`
Expand Down
44 changes: 43 additions & 1 deletion cmd/server/convert.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
ex "github.com/me-io/go-swap/pkg/exchanger"
"github.com/me-io/go-swap/pkg/swap"
"io/ioutil"
"math"
"net/http"
"time"
Expand All @@ -25,6 +27,46 @@ func (c convertReqObj) Hash() string {
}

var Convert = func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
ConvertPost(w, r)
}
if r.Method == "GET" {
ConvertGet(w, r)
}
}

var ConvertGet = func(w http.ResponseWriter, r *http.Request) {

query := r.URL.Query()
apiKey := query.Get("apiKey")
exchanger := query.Get("exchanger")
amount := query.Get("amount")
from := query.Get("from")
to := query.Get("to")
cacheTime := query.Get("cacheTime")

payload := fmt.Sprintf(`{
"amount": %s,
"exchanger": [
{
"name": "%s",
"apiKey": "%s"
}
],
"from": "%s",
"to": "%s",
"cacheTime":"%s"
}`, amount, exchanger, apiKey, from, to, cacheTime)

fmt.Println(payload)
bytePayload := []byte(payload)
bytePayloadReader := bytes.NewReader(bytePayload)

r.Body = ioutil.NopCloser(bytePayloadReader)
ConvertPost(w, r)
}

var ConvertPost = func(w http.ResponseWriter, r *http.Request) {

convertReq := &convertReqObj{}

Expand Down Expand Up @@ -92,7 +134,7 @@ var Convert = func(w http.ResponseWriter, r *http.Request) {
convertRes.From = convertReq.From
convertRes.To = convertReq.To
convertRes.ExchangeValue = rate.GetValue()
convertRes.DateTime = rate.GetDateTime()
convertRes.RateDateTime = rate.GetDateTime()
convertRes.ExchangerName = rate.GetExchangerName()
convertRes.RateFromCache = false

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestConvertObj_Convert(t *testing.T) {
r := &http.Request{}
r.Body = ioutil.NopCloser(bytePayloadReader)

Convert(w, r)
ConvertPost(w, r)
assert.Contains(t, testResponse, expectedName[k])
assert.Contains(t, testResponse, expectedRateCache[k])
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/public/swagger/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ definitions:
type: "number"
format: "float"
example: 27.5814
ConvertedText:
convertedText:
type: "string"
example: "1 USD is worth 3.675 AED"
dateTime:
type: "string"
example: "2018-09-25T12:17:17Z"
exchangerName:
type: "string"
example: "google"
rateDateTime:
type: "string"
example: "2018-09-25T12:17:17Z"
rateFromCache:
type: "boolean"
example: false
Expand Down
6 changes: 3 additions & 3 deletions cmd/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ type convertReqObj struct {
type convertResObj struct {
From string `json:"from"`
To string `json:"to"`
OriginalAmount float64 `json:"originalAmount"`
ExchangerName string `json:"exchangerName"`
ExchangeValue float64 `json:"exchangeValue"`
OriginalAmount float64 `json:"originalAmount"`
ConvertedAmount float64 `json:"convertedAmount"`
ConvertedText string `json:"convertedText"`
DateTime string `json:"dateTime"`
ExchangerName string `json:"exchangerName"`
RateDateTime string `json:"rateDateTime"`
RateFromCache bool `json:"rateFromCache"`
}

Expand Down

0 comments on commit 7a5d00f

Please sign in to comment.