Untitled

mail@pastecode.io avatar
unknown
golang
2 years ago
866 B
1
Indexable
Never
func Post(api interface{}) (*http.Response, error) {
	body, err := json.Marshal(api)
	if err != nil {
		return nil, err
	}

	resp, err := http.Post("", "", bytes.NewBuffer(body))
	if err != nil {
		return nil, err
	}

	return resp, err
}

func DecodeHttpStatus(response *http.Response) int {
	return response.StatusCode
}

func DecodeResponseBody(response *http.Response, str interface{}) error{
	err := json.NewDecoder(response.Body).Decode(&str)
	if err != nil {
		return err
	}
	return nil
}

func StatusAndBody(api interface{}, apiResponse interface{}) int {
	post, err := Post(api)
	if err != nil {
		return 0
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			fmt.Println("HANDLE ME")
		}
	}(post.Body)
	status := DecodeHttpStatus(post)
	err = DecodeResponseBody(post, &apiResponse)
	if err != nil {
		return 0
	}
	return status
}