-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from screwdriver-cd/buildfromid
Started screwdriver.API.BuildFromID
- Loading branch information
Showing
4 changed files
with
221 additions
and
48 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
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 screwdriver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// API is a Screwdriver API endpoint | ||
type API interface { | ||
BuildFromID(buildID string) (Build, error) | ||
} | ||
|
||
type api struct { | ||
url string | ||
token string | ||
client *http.Client | ||
} | ||
|
||
// New returns a new API object | ||
func New(url, token string) (API, error) { | ||
api := api{ | ||
url, | ||
token, | ||
&http.Client{}, | ||
} | ||
return API(api), nil | ||
} | ||
|
||
// Build is a Screwdriver Build | ||
type Build struct { | ||
ID string `json:"id"` | ||
JobID string `json:"jobId"` | ||
} | ||
|
||
func (a api) get(url string) ([]byte, error) { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("Generating request to Screwdriver: %v", err) | ||
} | ||
token := fmt.Sprintf("Bearer %s", a.token) | ||
req.Header.Set("Authorization", token) | ||
|
||
response, err := a.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("Reading response from Screwdriver: %v", err) | ||
} | ||
|
||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("Reading response Body from Screwdriver: %v", err) | ||
} | ||
return body, nil | ||
} | ||
|
||
// BuildFromID fetches and returns a Build object from its ID | ||
func (a api) BuildFromID(buildID string) (build Build, err error) { | ||
url := fmt.Sprintf("%s/builds/%s", a.url, buildID) | ||
body, err := a.get(url) | ||
if err != nil { | ||
return build, fmt.Errorf("Reading response Body from Screwdriver: %v", err) | ||
} | ||
|
||
err = json.Unmarshal(body, &build) | ||
if err != nil { | ||
return build, fmt.Errorf("Parsing JSON response %q: %v", body, err) | ||
} | ||
return build, nil | ||
} |
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,68 @@ | ||
package screwdriver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func makeFakeHTTPClient(code int, body string, validator func(req *http.Request)) *http.Client { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
validator(r) | ||
w.WriteHeader(code) | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintln(w, body) | ||
})) | ||
|
||
transport := &http.Transport{ | ||
Proxy: func(req *http.Request) (*url.URL, error) { | ||
return url.Parse(server.URL) | ||
}, | ||
} | ||
|
||
return &http.Client{Transport: transport} | ||
} | ||
|
||
func validateHeader(t *testing.T, key, value string) func(r *http.Request) { | ||
return func(r *http.Request) { | ||
headers, ok := r.Header[key] | ||
if !ok { | ||
t.Fatalf("No %s header sent in Screwdriver request", key) | ||
} | ||
header := headers[0] | ||
if header != value { | ||
t.Errorf("%s header = %q, want %q", key, header, value) | ||
} | ||
} | ||
} | ||
|
||
func TestFromBuildId(t *testing.T) { | ||
want := Build{ | ||
ID: "testId", | ||
JobID: "testJob", | ||
} | ||
json, err := json.Marshal(want) | ||
if err != nil { | ||
t.Fatalf("Unable to Marshal JSON for test: %v", err) | ||
} | ||
|
||
wantToken := "faketoken" | ||
wantTokenHeader := fmt.Sprintf("Bearer %s", wantToken) | ||
|
||
validatorFunc := validateHeader(t, "Authorization", wantTokenHeader) | ||
http := makeFakeHTTPClient(200, string(json), validatorFunc) | ||
|
||
testAPI := api{"http://fakeurl", wantToken, http} | ||
build, err := testAPI.BuildFromID(want.ID) | ||
|
||
if err != nil { | ||
t.Errorf("Unexpected error from BuildFromID: %v", err) | ||
} | ||
|
||
if build != want { | ||
t.Errorf("build == %#v, want %#v", build, want) | ||
} | ||
} |