Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Proxy settings for specialize environment #16

Merged
merged 6 commits into from Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -48,6 +48,17 @@ c.BaseURL = "https://your.host.name/v1"
...
```

Setting a custom HipChat Server with customize proxy settings:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“If you need to use a transport other than http.DefaultTransport:”

```go
c := hipchat.NewClient("<AUTH TOKEN>")

proxyURL, err := url.Parse("<PROXY_URL:PROXY_PORT>")
if err != nil {
log.Printf("Expected no error, but got %q", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.Fatalf

}
c.Transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
```

Contributors
------------
+ Akshay Shah ([@akshayjshah](https://github.com/akshayjshah))
Expand All @@ -56,3 +67,4 @@ Contributors
+ Edward Muller ([@freeformz](https://github.com/freeformz))
+ Matt Blair ([@mblair](https://github.com/mblair))
+ Gordon Goetz ([@gtosh4](https://github.com/gtosh4))
+ Paras Patel ([@patelparas](https://github.com/PatelParas))
42 changes: 38 additions & 4 deletions hipchat.go
Expand Up @@ -2,12 +2,14 @@
package hipchat

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)

const (
Expand Down Expand Up @@ -86,13 +88,15 @@ type ErrorResponse struct {
type Client struct {
AuthToken string
BaseURL string
Timeout time.Duration
Transport http.RoundTripper
}

// NewClient allocates and returns a Client with the given authToken.
// By default, the client will use the publicly available HipChat servers.
// For internal or custom servers, set the BaseURL field of the Client.
func NewClient(authToken string) Client {
return Client{AuthToken: authToken, BaseURL: defaultBaseURL}
return Client{AuthToken: authToken, BaseURL: defaultBaseURL, Transport: http.DefaultTransport}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

place each property on its own line

return Client {
  AuthToken: authToken,
  ...
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to do this.

}

func urlValuesFromMessageRequest(req MessageRequest) (url.Values, error) {
Expand Down Expand Up @@ -130,10 +134,21 @@ func (c *Client) PostMessage(req MessageRequest) error {
return err
}

resp, err := http.PostForm(uri, payload)
reqs, err := http.NewRequest("POST", uri, bytes.NewBufferString(payload.Encode()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of bytes.NewBufferString(payload.Encode()), use strings.NewReader(payload.Encode())

reqs.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return err
}
client := &http.Client{
Transport: c.Transport,
Timeout: c.Timeout,
}

resp, err := client.Do(reqs)
if err != nil {
return err
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -168,10 +183,20 @@ func (c *Client) RoomHistory(id, date, tz string) ([]Message, error) {
uri := fmt.Sprintf("%s/rooms/history?auth_token=%s&room_id=%s&date=%s&timezone=%s",
c.BaseURL, url.QueryEscape(c.AuthToken), url.QueryEscape(id), url.QueryEscape(date), url.QueryEscape(tz))

resp, err := http.Get(uri)
reqs, err := http.NewRequest("GET", uri, nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop the trailing s from reqs, so that it’s req.

Here and elsewhere you’ve made the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed req ==> reqs because req is already used by MessageRequest. We need to change either of the object name.
or may be i can use hreq for http type of request object.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is fine.

if err != nil {
return nil, err
}
client := &http.Client{
Transport: c.Transport,
Timeout: c.Timeout,
}

resp, err := client.Do(reqs)
if err != nil {
return nil, err
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand All @@ -181,6 +206,7 @@ func (c *Client) RoomHistory(id, date, tz string) ([]Message, error) {
if resp.StatusCode != http.StatusOK {
return nil, getError(body)
}

msgResp := &struct{ Messages []Message }{}
if err := json.Unmarshal(body, msgResp); err != nil {
return nil, err
Expand All @@ -195,7 +221,15 @@ func (c *Client) RoomList() ([]Room, error) {
}
uri := fmt.Sprintf("%s/rooms/list?auth_token=%s", c.BaseURL, url.QueryEscape(c.AuthToken))

resp, err := http.Get(uri)
reqs, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
client := &http.Client{
Transport: c.Transport,
Timeout: c.Timeout,
}
resp, err := client.Do(reqs)
if err != nil {
return nil, err
}
Expand Down