forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
d3189e2
commit b76ec15
Showing
11 changed files
with
136 additions
and
156 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
.idea/ | ||
*.o | ||
*.a | ||
*.so | ||
|
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 |
---|---|---|
|
@@ -108,4 +108,4 @@ func TestReadJsonResult(t *testing.T) { | |
} else { | ||
t.Fatal("Fail to unmarshal JSON: empty res") | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package mailjet | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type httpClient struct { | ||
apiKeyPublic string | ||
apiKeyPrivate string | ||
headers map[string]string | ||
request *http.Request | ||
response interface{} | ||
} | ||
|
||
// APIKeyPublic returns the public key. | ||
func (c *httpClient) APIKeyPublic() string { | ||
return c.apiKeyPublic | ||
} | ||
|
||
// APIKeyPrivate returns the secret key. | ||
func (c *httpClient) APIKeyPrivate() string { | ||
return c.apiKeyPrivate | ||
} | ||
|
||
func (c *httpClient) Send(req *http.Request) *httpClient { | ||
c.request = req | ||
return c | ||
} | ||
|
||
func (c *httpClient) With(headers map[string]string) *httpClient { | ||
c.headers = headers | ||
return c | ||
} | ||
|
||
func (c *httpClient) Read(response interface{}) *httpClient { | ||
c.response = response | ||
return c | ||
} | ||
|
||
func (c *httpClient) Call() (count, total int, err error) { | ||
for key, value := range c.headers { | ||
c.request.Header.Add(key, value) | ||
} | ||
|
||
resp, err := c.doRequest(c.request) | ||
if resp != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
if err != nil { | ||
return count, total, err | ||
} else if resp == nil { | ||
return count, total, fmt.Errorf("empty response") | ||
} | ||
|
||
if c.response != nil { | ||
if resp.Header["Content-Type"] != nil { | ||
contentType := resp.Header["Content-Type"][0] | ||
if contentType == "application/json" { | ||
return readJSONResult(resp.Body, c.response) | ||
} else if contentType == "text/csv" { | ||
c.response, err = csv.NewReader(resp.Body).ReadAll() | ||
} | ||
} | ||
} | ||
|
||
return count, total, err | ||
} |
Oops, something went wrong.