-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
11 changed files
with
331 additions
and
7 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
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
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
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 |
---|---|---|
@@ -1,3 +1,134 @@ | ||
package exchanger | ||
|
||
//todo | ||
import ( | ||
"fmt" | ||
"github.com/bitly/go-simplejson" | ||
"io/ioutil" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type oneForgeApi struct { | ||
attributes | ||
} | ||
|
||
var ( | ||
oneForgeApiUrl = `https://forex.1forge.com/%s/convert?from=%s&to=%s&quantity=1&api_key=%s` | ||
oneForgeApiHeaders = map[string][]string{ | ||
`Accept`: {`text/html,application/xhtml+xml,application/xml,application/json`}, | ||
`Accept-Encoding`: {`text`}, | ||
} | ||
) | ||
|
||
func (c *oneForgeApi) requestRate(from string, to string, opt ...interface{}) (*oneForgeApi, error) { | ||
|
||
// todo add option opt to add more headers or client configurations | ||
// free mem-leak | ||
// optimize for memory leak | ||
// todo optimize curl connection | ||
|
||
url := fmt.Sprintf(oneForgeApiUrl, c.apiVersion, from, to, c.apiKey) | ||
req, _ := http.NewRequest("GET", url, nil) | ||
|
||
oneForgeApiHeaders[`User-Agent`] = []string{c.userAgent} | ||
req.Header = oneForgeApiHeaders | ||
|
||
res, err := c.Client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// free mem-leak | ||
// todo discard data | ||
c.responseBody = string(body) | ||
return c, nil | ||
} | ||
|
||
func (c *oneForgeApi) GetValue() float64 { | ||
return c.rateValue | ||
} | ||
|
||
func (c *oneForgeApi) GetDateTime() string { | ||
return c.rateDate.Format(time.RFC3339) | ||
} | ||
|
||
func (c *oneForgeApi) GetExchangerName() string { | ||
return c.name | ||
} | ||
|
||
func (c *oneForgeApi) Latest(from string, to string, opt ...interface{}) error { | ||
|
||
_, err := c.requestRate(from, to, opt) | ||
if err != nil { | ||
log.Print(err) | ||
return err | ||
} | ||
|
||
// if from currency is same as converted currency return value of 1 | ||
if from == to { | ||
c.rateValue = 1 | ||
return nil | ||
} | ||
|
||
json, err := simplejson.NewJson([]byte(c.responseBody)) | ||
|
||
if err != nil { | ||
log.Print(err) | ||
return err | ||
} | ||
|
||
// opening price | ||
value := json.GetPath(`value`). | ||
MustFloat64() | ||
// todo handle error | ||
c.rateValue = value | ||
c.rateDate = time.Now() | ||
return nil | ||
} | ||
|
||
func NewOneForgeApi(opt map[string]string) *oneForgeApi { | ||
keepAliveTimeout := 600 * time.Second | ||
timeout := 5 * time.Second | ||
defaultTransport := &http.Transport{ | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: keepAliveTimeout, | ||
DualStack: true, | ||
}).DialContext, | ||
MaxIdleConns: 100, | ||
MaxIdleConnsPerHost: 100, | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: defaultTransport, | ||
Timeout: timeout, | ||
} | ||
|
||
attr := attributes{ | ||
name: `1forge`, | ||
Client: client, | ||
apiVersion: `1.0.3`, | ||
apiKey: opt[`apiKey`], | ||
userAgent: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0`, | ||
} | ||
|
||
if opt[`userAgent`] != "" { | ||
attr.userAgent = opt[`userAgent`] | ||
} | ||
|
||
if opt[`apiVersion`] != "" { | ||
attr.apiVersion = opt[`apiVersion`] | ||
} | ||
|
||
r := &oneForgeApi{attr} | ||
return r | ||
} |
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 |
---|---|---|
@@ -1,3 +1,20 @@ | ||
package exchanger | ||
|
||
//todo | ||
import ( | ||
"github.com/me-io/go-swap/test/staticMock" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestOneForgeApi_Latest(t *testing.T) { | ||
rate := NewOneForgeApi(nil) | ||
assert.Equal(t, rate.name, `1forge`) | ||
|
||
rate.Client.Transport = staticMock.NewTestMT() | ||
|
||
rate.Latest(`EUR`, `EUR`) | ||
assert.Equal(t, float64(1), rate.GetValue()) | ||
|
||
rate.Latest(`USD`, `AED`) | ||
assert.Equal(t, float64(3.675), rate.GetValue()) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,137 @@ | ||
package exchanger | ||
|
||
//todo | ||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// @link : https://themoneyconverter.com/AED/EUR.aspx?amount=1 | ||
|
||
type theMoneyConverterApi struct { | ||
attributes | ||
} | ||
|
||
var ( | ||
theMoneyConverterApiUrl = `https://themoneyconverter.com/%s/%s.aspx?amount=1` | ||
theMoneyConverterApiHeaders = map[string][]string{ | ||
`Accept`: {`text/html`}, | ||
} | ||
) | ||
|
||
func (c *theMoneyConverterApi) requestRate(from string, to string, opt ...interface{}) (*theMoneyConverterApi, error) { | ||
|
||
// todo add option opt to add more headers or client configurations | ||
// free mem-leak | ||
// optimize for memory leak | ||
// todo optimize curl connection | ||
|
||
// format the url and replace currency | ||
url := fmt.Sprintf(theMoneyConverterApiUrl, from, to) | ||
// prepare the request | ||
req, _ := http.NewRequest("GET", url, nil) | ||
// assign the request headers | ||
theMoneyConverterApiHeaders[`User-Agent`] = []string{c.userAgent} | ||
req.Header = theMoneyConverterApiHeaders | ||
|
||
// execute the request | ||
res, err := c.Client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// free mem-leak | ||
// todo discard data | ||
c.responseBody = string(body) | ||
return c, nil | ||
} | ||
|
||
func (c *theMoneyConverterApi) GetValue() float64 { | ||
return c.rateValue | ||
} | ||
|
||
func (c *theMoneyConverterApi) GetDateTime() string { | ||
return c.rateDate.Format(time.RFC3339) | ||
} | ||
|
||
func (c *theMoneyConverterApi) GetExchangerName() string { | ||
return c.name | ||
} | ||
|
||
func (c *theMoneyConverterApi) Latest(from string, to string, opt ...interface{}) error { | ||
|
||
// todo cache layer | ||
_, err := c.requestRate(from, to, opt) | ||
if err != nil { | ||
log.Print(err) | ||
return err | ||
} | ||
|
||
// if from currency is same as converted currency return value of 1 | ||
if from == to { | ||
c.rateValue = 1 | ||
return nil | ||
} | ||
|
||
validID := regexp.MustCompile(`(?s)output(.*)>(.*)</output>`) | ||
stringMatches := validID.FindStringSubmatch(c.responseBody) | ||
|
||
stringMatch := strings.TrimSpace(strings.Replace(stringMatches[2], "\n", "", -1)) | ||
stringMatch = strings.Replace(stringMatch, fmt.Sprintf("%d %s = ", 1, from), "", -1) | ||
stringMatch = strings.Replace(stringMatch, fmt.Sprintf(" %s", to), "", -1) | ||
|
||
c.rateValue, err = strconv.ParseFloat(stringMatch, 64) | ||
c.rateDate = time.Now() | ||
|
||
if err != nil { | ||
log.Print(err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewTheMoneyConverterApi(opt map[string]string) *theMoneyConverterApi { | ||
|
||
keepAliveTimeout := 600 * time.Second | ||
timeout := 5 * time.Second | ||
defaultTransport := &http.Transport{ | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: keepAliveTimeout, | ||
DualStack: true, | ||
}).DialContext, | ||
MaxIdleConns: 100, | ||
MaxIdleConnsPerHost: 100, | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: defaultTransport, | ||
Timeout: timeout, | ||
} | ||
|
||
attr := attributes{ | ||
name: `themoneyconverter`, | ||
Client: client, | ||
userAgent: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0`, | ||
} | ||
if opt[`userAgent`] != "" { | ||
attr.userAgent = opt[`userAgent`] | ||
} | ||
|
||
r := &theMoneyConverterApi{attr} | ||
|
||
return r | ||
} |
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 |
---|---|---|
@@ -1,5 +1,20 @@ | ||
package exchanger | ||
|
||
//todo | ||
import ( | ||
"github.com/me-io/go-swap/test/staticMock" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// @link : https://themoneyconverter.com/AED/EUR.aspx?amount=1 | ||
func TestTheMoneyConverterApi_Latest(t *testing.T) { | ||
rate := NewTheMoneyConverterApi(nil) | ||
assert.Equal(t, rate.name, `themoneyconverter`) | ||
|
||
rate.Client.Transport = staticMock.NewTestMT() | ||
|
||
rate.Latest(`EUR`, `EUR`) | ||
assert.Equal(t, float64(1), rate.GetValue()) | ||
|
||
rate.Latest(`USD`, `AED`) | ||
assert.Equal(t, float64(3.6725), rate.GetValue()) | ||
} |
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
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,5 @@ | ||
{ | ||
"value": 3.675, | ||
"text": "1 USD is worth 3.675 AED", | ||
"timestamp": 1538254496 | ||
} |
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,9 @@ | ||
<div class="cc-result"> | ||
<output id="res1"> | ||
1 USD = 3.67250 AED | ||
</output> | ||
</div> | ||
<div class="invert-currencies"> | ||
<a href="/AED/USD.aspx" id="ivt" title="Switch currencies">invert currencies</a> | ||
<span class="large">↓↑</span> | ||
</div> |
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