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
Changes from 4 commits
593546f
f3056fa
02d9fda
8d16f00
6095c78
61dc0bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,17 @@ c.BaseURL = "https://your.host.name/v1" | |
... | ||
``` | ||
|
||
Setting a custom HipChat Server with customize proxy settings: | ||
```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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
@@ -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)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
package hipchat | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
const ( | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. place each property on its own line return Client {
AuthToken: authToken,
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still need to do this. |
||
} | ||
|
||
func urlValuesFromMessageRequest(req MessageRequest) (url.Values, error) { | ||
|
@@ -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())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of |
||
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 { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drop the trailing s from reqs, so that it’s Here and elsewhere you’ve made the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i changed There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
There was a problem hiding this comment.
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
:”