Untitled

 avatar
unknown
golang
4 years ago
867 B
7
Indexable
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{}) interface{} {
	err := json.NewDecoder(response.Body).Decode(&str)
	if err != nil {
		return err
	}
	return str
}

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