Skip to content

Commit 0fc99d9

Browse files
committed
chores: change PostParam and Headers input data type
1 parent c9b03bf commit 0fc99d9

File tree

4 files changed

+115
-96
lines changed

4 files changed

+115
-96
lines changed

README.md

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ go get -u github.com/ranjanrak/dead-letter-queue
3030
package main
3131

3232
import (
33-
"net/http"
34-
"net/url"
33+
"fmt"
34+
"net/http"
35+
"net/url"
3536

36-
deadletterqueue "github.com/ranjanrak/dead-letter-queue"
37+
deadletterqueue "github.com/ranjanrak/dead-letter-queue"
3738
)
3839
func main() {
3940
// Create new HTTP request queue instance
@@ -43,39 +44,44 @@ func main() {
4344
Ctx: nil,
4445
QueueName: "",
4546
DeadHTTP: []int{400, 403, 429, 500, 502},
46-
})
47+
})
48+
49+
// Add post params
50+
postParam := url.Values{}
51+
postParam.Add("exchange", "NSE")
52+
postParam.Add("tradingsymbol", "TCS")
53+
postParam.Add("transaction_type", "BUY")
54+
postParam.Add("quantity", "1")
55+
postParam.Add("product", "CNC")
56+
postParam.Add("order_type", "MARKET")
57+
postParam.Add("validity", "DAY")
58+
59+
// Add request header
60+
var headers http.Header = map[string][]string{}
61+
headers.Add("x-kite-version", "3")
62+
headers.Add("authorization", "token api_key:access_token")
63+
headers.Add("content-type", "application/x-www-form-urlencoded")
64+
4765
// Request message
48-
queueMsg := deadletterqueue.InputMsg{
49-
Name: "Place TCS Order",
50-
Url: "https://api.kite.trade/orders/regular",
51-
ReqMethod: "POST",
52-
PostParam: map[string]interface{}{
53-
"exchange": "NSE",
54-
"tradingsymbol": "TCS",
55-
"transaction_type": "BUY",
56-
"quantity": 1,
57-
"product": "CNC",
58-
"order_type": "MARKET",
59-
"validity": "DAY",
60-
},
61-
Headers: map[string]interface{}{
62-
"x-kite-version": 3,
63-
"authorization": "token abcd123:efgh1234",
64-
"content-type": "application/x-www-form-urlencoded",
65-
},
66-
}
67-
68-
// worker that adds message to http queue
69-
err := httpQueue.AddMessage(queueMsg)
70-
if err != nil {
71-
fmt.Printf("Error adding msg in the request queue : %v", err)
72-
}
73-
74-
// worker that executes http request queues
75-
httpQueue.ExecuteQueue()
76-
77-
// worker that executes only dead letter http queues
78-
httpQueue.ExecuteDeadQueue()
66+
reqMsgOrd := deadletterqueue.InputMsg{
67+
Name: "Place TCS Order",
68+
Url: "https://api.kite.trade/orders/regular",
69+
ReqMethod: "POST",
70+
PostParam: postParam,
71+
Headers: headers,
72+
}
73+
74+
// worker that adds message to redis queue
75+
err := httpQueue.AddMessage(reqMsgOrd)
76+
if err != nil {
77+
fmt.Printf("Error adding msg in the request queue : %v", err)
78+
}
79+
80+
// worker that executes http request queues
81+
httpQueue.ExecuteQueue()
82+
83+
// worker that executes dead letter http queues
84+
httpQueue.ExecuteDeadQueue()
7985
}
8086
```
8187

@@ -85,28 +91,21 @@ Request represents an HTTP request with all parameters.
8591

8692
### Adding message
8793

88-
Adding an HTTP message to the HTTP queue with all parameters.
94+
Adding an HTTP message to the request queue with all parameters.
8995

9096
```go
97+
// Add request header
98+
var headers http.Header = map[string][]string{}
99+
headers.Add("x-kite-version", "3")
100+
headers.Add("authorization", "token api_key:access_token")
101+
91102
// Request message
92103
queueMsg := deadletterqueue.InputMsg{
93-
Name: "Place TCS Order",
94-
Url: "https://api.kite.trade/orders/regular",
95-
ReqMethod: "POST",
96-
PostParam: map[string]interface{}{
97-
"exchange": "NSE",
98-
"tradingsymbol": "TCS",
99-
"transaction_type": "BUY",
100-
"quantity": 1,
101-
"product": "CNC",
102-
"order_type": "MARKET",
103-
"validity": "DAY",
104-
},
105-
Headers: map[string]interface{}{
106-
"x-kite-version": 3,
107-
"authorization": "token abcd123:efgh1234",
108-
"content-type": "application/x-www-form-urlencoded",
109-
},
104+
Name: "Fetch order book",
105+
Url: "https://api.kite.trade/orders",
106+
ReqMethod: "GET",
107+
PostParam: nil,
108+
Headers: headers,
110109
}
111110
err := httpQueue.AddMessage(queueMsg)
112111
if err != nil {
@@ -116,7 +115,7 @@ if err != nil {
116115

117116
### Delete message from the request queue
118117

119-
Delete request message available in HTTP queue before it's execution with the input message `Name`.
118+
Delete request message available in the queue before it's execution with the input message `Name`.
120119

121120
```go
122121
err := httpQueue.DeleteReqMsg("Place TCS Order")

dead_letter.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"io/ioutil"
109
"log"
@@ -29,8 +28,8 @@ type InputMsg struct {
2928
Name string
3029
Url string
3130
ReqMethod string
32-
PostParam map[string]interface{}
33-
Headers map[string]interface{}
31+
PostParam url.Values
32+
Headers http.Header
3433
}
3534

3635
// Client represents interface for redis queue
@@ -115,30 +114,19 @@ func (c *Client) ExecuteQueueName(qName string) {
115114

116115
// RawExecute performs the HTTP request based on request params
117116
func (c *Client) RawExecute(msgParam InputMsg, qName string) {
118-
// Add all POST or PUT params as url.values map
119-
postData := url.Values{}
120-
if msgParam.PostParam != nil {
121-
for key, value := range msgParam.PostParam {
122-
valueStr := fmt.Sprintf("%v", value)
123-
postData.Add(key, valueStr)
124-
}
125-
}
126117
var postBody io.Reader
127118
if msgParam.ReqMethod == "POST" || msgParam.ReqMethod == "PUT" {
128-
// convert post params map into “URL encoded” form
129-
paramsEncoded := postData.Encode()
130-
postBody = bytes.NewReader([]byte(paramsEncoded))
119+
// convert post params map into “URL encoded”
120+
if msgParam.PostParam != nil {
121+
paramsEncoded := msgParam.PostParam.Encode()
122+
postBody = bytes.NewReader([]byte(paramsEncoded))
123+
}
131124
}
132125
req, _ := http.NewRequest(msgParam.ReqMethod, msgParam.Url, postBody)
133126

134127
// Add all request headers to the http request
135-
reqHeader := http.Header{}
136128
if msgParam.Headers != nil {
137-
for name, value := range msgParam.Headers {
138-
valueStr := fmt.Sprintf("%v", value)
139-
reqHeader.Add(name, valueStr)
140-
}
141-
req.Header = reqHeader
129+
req.Header = msgParam.Headers
142130
}
143131

144132
res, err := http.DefaultClient.Do(req)

dead_letter_test.go

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"net/url"
710
"testing"
811

912
"github.com/go-redis/redis/v8"
@@ -33,25 +36,30 @@ func MockRedis() {
3336
func TestAddMessage(t *testing.T) {
3437
// Initialize the mock redis
3538
MockRedis()
39+
40+
// Add post params
41+
postParam := url.Values{}
42+
postParam.Add("exchange", "NSE")
43+
postParam.Add("tradingsymbol", "TCS")
44+
postParam.Add("transaction_type", "BUY")
45+
postParam.Add("quantity", "1")
46+
postParam.Add("product", "CNC")
47+
postParam.Add("order_type", "MARKET")
48+
postParam.Add("validity", "DAY")
49+
50+
// Add request header
51+
var headers http.Header = map[string][]string{}
52+
headers.Add("x-kite-version", "3")
53+
headers.Add("authorization", "token api_key:access_token")
54+
headers.Add("content-type", "application/x-www-form-urlencoded")
55+
3656
// Request message
3757
reqMsgOrd = InputMsg{
3858
Name: "Place TCS Order",
3959
Url: "https://api.kite.trade/orders/regular",
4060
ReqMethod: "POST",
41-
PostParam: map[string]interface{}{
42-
"exchange": "NSE",
43-
"tradingsymbol": "TCS",
44-
"transaction_type": "BUY",
45-
"quantity": 1,
46-
"product": "CNC",
47-
"order_type": "MARKET",
48-
"validity": "DAY",
49-
},
50-
Headers: map[string]interface{}{
51-
"x-kite-version": 3,
52-
"authorization": "token abcd123:efgh1234",
53-
"content-type": "application/x-www-form-urlencoded",
54-
},
61+
PostParam: postParam,
62+
Headers: headers,
5563
}
5664
// mock to set reqMsg for AddMessage call
5765
mock.ExpectSet("ReqQueue", structToJson([]InputMsg{reqMsgOrd}), 0).SetVal("OK")
@@ -61,19 +69,23 @@ func TestAddMessage(t *testing.T) {
6169
}
6270

6371
func TestDeleteReqMsg(t *testing.T) {
72+
// Add post params
73+
postParam := url.Values{}
74+
postParam.Add("api_key", "api_key")
75+
postParam.Add("request_token", "request_token")
76+
postParam.Add("checksum", "checksum")
77+
78+
// Add request header
79+
var headers http.Header = map[string][]string{}
80+
headers.Add("x-kite-version", "3")
81+
6482
// Request message
6583
reqMsgSess = InputMsg{
6684
Name: "Post session token",
6785
Url: "https://api.kite.trade/session/token",
6886
ReqMethod: "POST",
69-
PostParam: map[string]interface{}{
70-
"api_key": "api_key",
71-
"request_token": "request_token",
72-
"checksum": "checksum",
73-
},
74-
Headers: map[string]interface{}{
75-
"x-kite-version": 3,
76-
},
87+
PostParam: postParam,
88+
Headers: headers,
7789
}
7890
// Add mock to add both reqMsg and reqMsgSess in ReqQueue
7991
mock.ExpectGet("ReqQueue").SetVal(string(structToJson([]InputMsg{reqMsgOrd, reqMsgSess})))
@@ -100,6 +112,25 @@ func TestDeleteDeadMsg(t *testing.T) {
100112
assert.Nil(t, err)
101113
}
102114

115+
func TestMessageStatus(t *testing.T) {
116+
// Load mock response
117+
mockOrders, err := ioutil.ReadFile("./mockdata/orderbook_response.json")
118+
if err != nil {
119+
t.Errorf("Error while fetching orderbook_response. %v", err)
120+
}
121+
// Mock to fetch request status
122+
mock.ExpectGet("Fetch order book").SetVal(string(mockOrders))
123+
// Check response status for executed message
124+
response, err := cli.MessageStatus("Fetch order book")
125+
if err != nil {
126+
fmt.Printf("Error %v", err)
127+
}
128+
var mockStruct map[string]interface{}
129+
json.Unmarshal([]byte(response), &mockStruct)
130+
//assert
131+
assert.Equal(t, mockStruct["status"], "success", "Fetch order book request failed.")
132+
}
133+
103134
// structToString parses struct to json for redis mock
104135
func structToJson(msg []InputMsg) []byte {
105136
jsonMessage, err := json.Marshal(msg)

mockdata/orderbook_response.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":"success","data":[{"placed_by":"Test Client","order_id":"220516001538500","exchange_order_id":"1300000011463164","parent_order_id":null,"status":"COMPLETE","status_message":null,"status_message_raw":null,"order_timestamp":"2022-05-16 11:54:18","exchange_update_timestamp":"2022-05-16 11:54:18","exchange_timestamp":"2022-05-16 11:54:18","variety":"regular","modified":false,"exchange":"NSE","tradingsymbol":"SBIN","instrument_token":779521,"order_type":"MARKET","transaction_type":"BUY","validity":"DAY","validity_ttl":0,"product":"CNC","quantity":1,"disclosed_quantity":0,"price":0,"trigger_price":0,"average_price":455.5,"filled_quantity":1,"pending_quantity":0,"cancelled_quantity":0,"market_protection":0,"meta":{},"tag":null,"guid":"ABCDEFGHIJKLM0123"}]}

0 commit comments

Comments
 (0)