fix(pkg/hub): url encoded body

This commit is contained in:
Markus Pesch 2019-11-12 20:11:40 +01:00
parent 464070a02b
commit 691239f706
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 19 additions and 31 deletions

View File

@ -121,35 +121,30 @@ func PatchRepository(repository *types.Repository, token *types.Token) (*types.R
return nil, errorNoRepositoryDefined
}
repositoryBuffer := new(bytes.Buffer)
jsonEncoder := json.NewEncoder(repositoryBuffer)
if err := jsonEncoder.Encode(repository); err != nil {
return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
}
// repositoryBuffer := new(bytes.Buffer)
// jsonEncoder := json.NewEncoder(repositoryBuffer)
// if err := jsonEncoder.Encode(repository); err != nil {
// return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
// }
client := new(http.Client)
// patchURL, err := url.Parse(fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, repository.Namespcace, repository.Name))
// if err != nil {
// return nil, fmt.Errorf("Can not prase URL: %v", err)
// }
patchURL := "https://httpbin.org/patch"
patchURL, err := url.Parse(fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, repository.Namespcace, repository.Name))
if err != nil {
return nil, fmt.Errorf("Can not prase URL: %v", err)
}
data := url.Values{}
data.Set("full_description", repository.FullDescription)
req, err := http.NewRequest(http.MethodPatch, patchURL, strings.NewReader(data.Encode()))
req, err := http.NewRequest(http.MethodPatch, patchURL.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("Can not create request to update readme: %v", err)
return nil, fmt.Errorf("Can not create http request to update file: %v", err)
}
req.Header.Add("Accept", "*/*")
req.Header.Add("Authorization", fmt.Sprintf("JWT %v", token.Token))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
//req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
req.Header.Del("Accept-Encoding")
flogger.Debug("Content-Length", strconv.Itoa(len(data.Encode())))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
@ -157,17 +152,15 @@ func PatchRepository(repository *types.Repository, token *types.Token) (*types.R
}
defer resp.Body.Close()
flogger.Debug("Get Statuscode: %v", resp.StatusCode)
if resp.StatusCode == 200 {
if resp.StatusCode != 200 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
//return nil, fmt.Errorf("Invalid HTTP-Statuscode: Get %v but expect 200: %v", resp.StatusCode, string(bodyBytes))
flogger.Debug("RESP_BODY: %v", string(bodyBytes))
return nil, fmt.Errorf("Invalid HTTP-Statuscode: Get %v but expect 200: %v", resp.StatusCode, string(bodyBytes))
}
patchedRepository := new(types.Repository)
if err := json.NewDecoder(resp.Body).Decode(patchedRepository); err != nil {
jsonDecoder := json.NewDecoder(resp.Body)
if err := jsonDecoder.Decode(patchedRepository); err != nil {
return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
}

View File

@ -56,5 +56,5 @@ func TestPatchRepository(t *testing.T) {
require.NoError(err)
require.NotEqual(currentRepository, actualRepository, "The repository properties have remained the same even though an update was performed")
require.Equal(&expectedRepository, actualRepository, "The update was successfully")
require.EqualValues(&expectedRepository, actualRepository, "The update was successfully")
}

View File

@ -5,10 +5,6 @@ IFS=$'\n\t'
# Set the default path to README.md
README_FILEPATH=${README_FILEPATH:="./README.md"}
DOCKERHUB_PASSWORD=${DOCKER_PASSWORD}
DOCKERHUB_USERNAME=volkerraschek
DOCKERHUB_REPOSITORY=volkerraschek/dhd
# Acquire a token for the Docker Hub API
echo "Acquiring token"
LOGIN_PAYLOAD="{\"username\": \"${DOCKERHUB_USERNAME}\", \"password\": \"${DOCKERHUB_PASSWORD}\"}"
@ -16,8 +12,7 @@ TOKEN=$(curl -H "Content-Type: application/json" -X POST -d ${LOGIN_PAYLOAD} htt
# Send a PATCH request to update the description of the repository
echo "Sending PATCH request"
# REPO_URL="https://hub.docker.com/v2/repositories/${DOCKERHUB_REPOSITORY}/"
REPO_URL="https://httpbin.org/patch"
REPO_URL="https://hub.docker.com/v2/repositories/${DOCKERHUB_REPOSITORY}/"
RESPONSE_CODE=$(curl --write-out %{response_code} -H "Authorization: JWT ${TOKEN}" -X PATCH --data-urlencode full_description@${README_FILEPATH} ${REPO_URL})
echo "Received response code: $RESPONSE_CODE"