1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-06-23 00:07:40 +02:00

* Add ability to add and download attachments, including multi-part form handling

* Add method to report if the current session is authenticated or not
This commit is contained in:
Jason O'Broin
2016-05-19 14:11:21 -07:00
parent 559b76c3ef
commit 2bc0c88214
2 changed files with 141 additions and 11 deletions

60
jira.go
View File

@ -89,6 +89,34 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ
return req, nil
}
// NewMultiPartRequest creates an API request including a multi-part file
// A relative URL can be provided in urlStr, in which case it is resolved relative to the baseURL of the Client.
// Relative URLs should always be specified without a preceding slash.
// If specified, the value pointed to by buf is a multipart form
func (c *Client) NewMultiPartRequest(method, urlStr string, buf *bytes.Buffer) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := c.baseURL.ResolveReference(rel)
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
// Set required headers
req.Header.Set("X-Atlassian-Token", "nocheck")
// Set session cookie if there is one
if c.session != nil {
req.Header.Set("Cookie", fmt.Sprintf("%s=%s", c.session.Session.Name, c.session.Session.Value))
}
return req, nil
}
// Do sends an API request and returns the API response.
// The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
@ -113,6 +141,38 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
return resp, err
}
// Do sends an API request and returns the API response.
// The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
// The caller needs to call Body.Close when the response has been handled
func (c *Client) DoNoClose(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
err = CheckResponse(resp)
if err != nil {
// Even though there was an error, we still return the response
// in case the caller wants to inspect it further
return resp, err
}
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
}
return resp, err
}
// Authenticated reports if the current Client has an authenticated session with JIRA
func (c *Client) Authenticated() bool {
if c != nil {
return c.session != nil
} else {
return false
}
}
// CheckResponse checks the API response for errors, and returns them if present.
// A response is considered an error if it has a status code outside the 200 range.
// API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse.