Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Perform composition when zip format is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
odlp committed May 19, 2020
1 parent f5738ee commit 49698a6
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 61 deletions.
15 changes: 8 additions & 7 deletions client/client.go
Expand Up @@ -21,35 +21,36 @@ const bgImageFileParam = "bg_image_file"

//go:generate counterfeiter . ClientInterface
type ClientInterface interface {
RemoveFromFile(inputPath string, apiKey string, params map[string]string) ([]byte, error)
RemoveFromFile(inputPath string, apiKey string, params map[string]string) ([]byte, string, error)
}

type Client struct {
HTTPClient http.Client
}

func (c Client) RemoveFromFile(inputPath string, apiKey string, params map[string]string) ([]byte, error) {
func (c Client) RemoveFromFile(inputPath string, apiKey string, params map[string]string) ([]byte, string, error) {
request, err := buildRequest(APIEndpoint, apiKey, params, inputPath)
if err != nil {
return nil, err
return nil, "", err
}

resp, err := c.HTTPClient.Do(request)
if err != nil {
return nil, err
return nil, "", err
}

defer resp.Body.Close()

statusCode := resp.StatusCode
body, err := ioutil.ReadAll(resp.Body)
contentType := resp.Header.Get("Content-Type")

if statusCode == 200 {
return body, err
return body, contentType, err
} else if statusCode >= 400 && statusCode < 500 {
return nil, parseJsonErrors(body)
return nil, "", parseJsonErrors(body)
} else {
return nil, fmt.Errorf("Unable to process image http_status=%d", resp.StatusCode)
return nil, "", fmt.Errorf("Unable to process image http_status=%d", resp.StatusCode)
}
}

Expand Down
14 changes: 8 additions & 6 deletions client/client_test.go
Expand Up @@ -36,12 +36,14 @@ var _ = Describe("Client", func() {
Post("/v1.0/removebg").
MatchHeader("X-Api-Key", "^api-key$").
Reply(200).
SetHeader("Content-Type", "image/png").
BodyString("data")

result, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
result, contentType, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})

Expect(err).To(Not(HaveOccurred()))
Expect(result).To(Equal([]byte("data")))
Expect(contentType).To(Equal("image/png"))
Expect(gock.IsDone()).To(BeTrue())
})

Expand All @@ -54,7 +56,7 @@ var _ = Describe("Client", func() {
Reply(200).
BodyString("data")

_, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
_, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})

Expect(err).To(Not(HaveOccurred()))
Expect(gock.IsDone()).To(BeTrue())
Expand All @@ -75,7 +77,7 @@ var _ = Describe("Client", func() {
"bg_image_file": bgFixtureFile,
}

_, err := subject.RemoveFromFile(fixtureFile, "api-key", params)
_, _, err := subject.RemoveFromFile(fixtureFile, "api-key", params)

Expect(err).To(Not(HaveOccurred()))
Expect(gock.IsDone()).To(BeTrue())
Expand All @@ -99,7 +101,7 @@ var _ = Describe("Client", func() {
Post("/v1.0/removebg").
Reply(500)

result, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
result, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})

Expect(result).To(BeNil())
Expect(err).To(MatchError("Unable to process image http_status=500"))
Expand All @@ -115,7 +117,7 @@ var _ = Describe("Client", func() {
Reply(400).
BodyString(jsonError)

result, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
result, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})

Expect(result).To(BeNil())
Expect(err).To(MatchError("File too large, Second error"))
Expand All @@ -125,7 +127,7 @@ var _ = Describe("Client", func() {
Context("input file doesn't exist", func() {
It("returns a clear error", func() {
nonExistentFile := "/tmp/not-a-file"
result, err := subject.RemoveFromFile(nonExistentFile, "api-key", map[string]string{})
result, _, err := subject.RemoveFromFile(nonExistentFile, "api-key", map[string]string{})

Expect(result).To(BeNil())
Expect(err).To(MatchError("Unable to read file"))
Expand Down
33 changes: 19 additions & 14 deletions client/clientfakes/fake_client_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions composite/composite.go
Expand Up @@ -13,6 +13,11 @@ import (
"io"
)

//go:generate counterfeiter . CompositorInterface
type CompositorInterface interface {
Process(inputZipPath string, outputImagePath string) error
}

type Compositor struct {
Storage storage.StorageInterface
}
Expand Down
112 changes: 112 additions & 0 deletions composite/compositefakes/fake_compositor_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49698a6

Please sign in to comment.