Skip to content

Commit

Permalink
Merge pull request #105 from skatsaounis/feat-put-files
Browse files Browse the repository at this point in the history
feat: add methods to put and update with files
  • Loading branch information
manadart authored Jun 7, 2024
2 parents 39eac57 + 2435e09 commit 0fb6099
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ func (client Client) Post(uri *url.URL, operation string, parameters url.Values,
}

// Put updates an object on the API, using an HTTP "PUT" request.
func (client Client) Put(uri *url.URL, parameters url.Values) ([]byte, error) {
func (client Client) Put(uri *url.URL, parameters url.Values, files map[string][]byte) ([]byte, error) {
if files != nil {
return client.nonIdempotentRequestFiles("PUT", uri, parameters, files)
}
return client.nonIdempotentRequest("PUT", uri, parameters)
}

Expand Down
27 changes: 17 additions & 10 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,27 @@ func (suite *ClientSuite) TestClientPostSendsRequestWithParams(c *gc.C) {
c.Check(postedValues, jc.DeepEquals, expectedPostedValues)
}

// extractFileContent extracts from the request built using 'requestContent',
// extractFileContentAndParamValue extracts from the request built using 'requestContent',
// 'requestHeader' and 'requestURL', the file named 'filename'.
func extractFileContent(requestContent string, requestHeader *http.Header, requestURL string, _ string) ([]byte, error) {
// In addition, it extracts the url.Value of the given 'key'.
func extractFileContentAndParamValue(requestContent string, requestHeader *http.Header, requestURL string, filename string, key string) ([]byte, string, error) {
// Recreate the request from server.requestContent to use the parsing
// utility from the http package (http.Request.FormFile).
request, err := http.NewRequest("POST", requestURL, bytes.NewBufferString(requestContent))
if err != nil {
return nil, err
return nil, "", err
}
request.Header.Set("Content-Type", requestHeader.Get("Content-Type"))
file, _, err := request.FormFile("testfile")
file, _, err := request.FormFile(filename)
if err != nil {
return nil, err
return nil, "", err
}
fileContent, err := io.ReadAll(file)
if err != nil {
return nil, err
return nil, "", err
}
return fileContent, nil

return fileContent, request.FormValue(key), nil
}

func (suite *ClientSuite) TestClientPostSendsMultipartRequest(c *gc.C) {
Expand All @@ -313,7 +315,7 @@ func (suite *ClientSuite) TestClientPostSendsMultipartRequest(c *gc.C) {

c.Assert(err, jc.ErrorIsNil)
c.Check(string(result), gc.Equals, expectedResult)
receivedFileContent, err := extractFileContent(*server.requestContent, server.requestHeader, fullURI, "testfile")
receivedFileContent, _, err := extractFileContentAndParamValue(*server.requestContent, server.requestHeader, fullURI, "testfile", "")
c.Assert(err, jc.ErrorIsNil)
c.Check(receivedFileContent, jc.DeepEquals, fileContent)
}
Expand All @@ -327,12 +329,17 @@ func (suite *ClientSuite) TestClientPutSendsRequest(c *gc.C) {
defer server.Close()
client, err := NewAnonymousClient(server.URL, "1.0")
c.Assert(err, jc.ErrorIsNil)
fileContent := []byte("content")
files := map[string][]byte{"testfile": fileContent}

result, err := client.Put(URI, params)
result, err := client.Put(URI, params, files)

c.Assert(err, jc.ErrorIsNil)
c.Check(string(result), gc.Equals, expectedResult)
c.Check(*server.requestContent, gc.Equals, "test=123")
receivedFileContent, value, err := extractFileContentAndParamValue(*server.requestContent, server.requestHeader, URI.String(), "testfile", "test")
c.Assert(err, jc.ErrorIsNil)
c.Check(receivedFileContent, jc.DeepEquals, fileContent)
c.Check(value, gc.Equals, "123")
}

func (suite *ClientSuite) TestClientDeleteSendsRequest(c *gc.C) {
Expand Down
2 changes: 1 addition & 1 deletion controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ func (c *controller) put(path string, params url.Values) (interface{}, error) {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
logger.Tracef("request %x: PUT %s%s, params: %s", requestID, c.client.APIURL, path, params.Encode())
bytes, err := c.client.Put(&url.URL{Path: path}, params)
bytes, err := c.client.Put(&url.URL{Path: path}, params, nil)
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
Expand Down
17 changes: 16 additions & 1 deletion maasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,22 @@ func (obj MAASObject) Post(params url.Values) (JSONObject, error) {
// "params." It returns the object's new value as received from the API.
func (obj MAASObject) Update(params url.Values) (MAASObject, error) {
uri := obj.URI()
result, err := obj.client.Put(uri, params)
result, err := obj.client.Put(uri, params, nil)
if err != nil {
return MAASObject{}, err
}
jsonObj, err := Parse(obj.client, result)
if err != nil {
return MAASObject{}, err
}
return jsonObj.GetMAASObject()
}

// UpdateFiles modifies this object on the API, based on the values given in
// "params." It returns the object's new value as received from the API.
func (obj MAASObject) UpdateFiles(params url.Values, files map[string][]byte) (MAASObject, error) {
uri := obj.URI()
result, err := obj.client.Put(uri, params, files)
if err != nil {
return MAASObject{}, err
}
Expand Down

0 comments on commit 0fb6099

Please sign in to comment.