Verbose flag added
This commit is contained in:
parent
c5811d75b7
commit
020ac7a6fb
3 changed files with 48 additions and 1 deletions
|
|
@ -1,7 +1,10 @@
|
|||
package jkl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
|
|
@ -27,18 +30,44 @@ func NewJiraClient(jiraRoot string) *JiraClient {
|
|||
if j.jiraRoot == "" {
|
||||
j.jiraRoot = os.Getenv("JIRA_ROOT")
|
||||
}
|
||||
if *Verbose {
|
||||
fmt.Println("Jira root:", j.jiraRoot)
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
func (j *JiraClient) Do(req *http.Request) (*http.Response, error) {
|
||||
var err error
|
||||
req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_PASSWORD"))
|
||||
if *Verbose {
|
||||
fmt.Println("Jira User: ", os.Getenv("JIRA_USER"))
|
||||
fmt.Println("Jira Password: ", os.Getenv("JIRA_PASSWORD"))
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Accept", "application/json, text/plain, text/html")
|
||||
req.URL, err = url.Parse(j.jiraRoot + "rest/" + req.URL.RequestURI())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return j.Client.Do(req)
|
||||
resp, err := j.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
fmt.Println("Status code:", resp.StatusCode)
|
||||
if *Verbose {
|
||||
fmt.Println("Headers:")
|
||||
fmt.Println(resp.Header)
|
||||
}
|
||||
fmt.Println("Response:")
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
return nil, errors.New("Some http error happened.")
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (j *JiraClient) Put(path string, payload io.Reader) (*http.Response, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue