<commit_before>// Copyright (c) 2019, The Decred developers
// See LICENSE for details.

// Package piclient handles the http requests made to Politeia APIs.
package piclient

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"

	pitypes "github.com/decred/dcrdata/v4/gov/politeia/types"
	piapi "github.com/decred/politeia/politeiawww/api/v1"
)

// HandleGetRequests accepts a http client and API URL path as arguments. If the
// parameters are valid, a GET request is made to the API URL passed. The body
// returned is decoded into []byte and returned.
func HandleGetRequests(client *http.Client, URLPath string) ([]byte, error) {
	if client == nil {
		return nil, fmt.Errorf("invalid http client was passed")
	}

	if URLPath == "" {
		return nil, fmt.Errorf("empty API URL is not supported")
	}

	response, err := client.Get(URLPath)
	if err != nil {
		return nil, err
	}

	defer response.Body.Close()

	return ioutil.ReadAll(response.Body)
}

// DropURLRegex replaces "{token:[A-z0-9]{64}}" in a URL with provided the parameter.
func DropURLRegex(URLPath, param string) string {
	r := regexp.MustCompile(`\{token:\[A-z0-9]\{64\}}`)
	return r.ReplaceAllLiteralString(URLPath, param)
}
<commit_msg>Check for valid htp status code<commit_after>// Copyright (c) 2019, The Decred developers
// See LICENSE for details.

// Package piclient handles the http requests made to Politeia APIs.
package piclient

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"

	pitypes "github.com/decred/dcrdata/v4/gov/politeia/types"
	piapi "github.com/decred/politeia/politeiawww/api/v1"
)

// HandleGetRequests accepts a http client and API URL path as arguments. If the
// parameters are valid, a GET request is made to the API URL passed. The body
// returned is decoded into []byte and returned.
func HandleGetRequests(client *http.Client, URLPath string) ([]byte, error) {
	if client == nil {
		return nil, fmt.Errorf("invalid http client was passed")
	}

	if URLPath == "" {
		return nil, fmt.Errorf("empty API URL is not supported")
	}

	response, err := client.Get(URLPath)
	if err != nil || response == nil {
		return nil, fmt.Errorf("request failed: %v", err)
	}

	// Check if valid status code (200 Ok) was returned.
	if response.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("request (%s) failed with status code: %s",
			URLPath, response.Status)
	}

	defer response.Body.Close()

	return ioutil.ReadAll(response.Body)
}

// DropURLRegex replaces "{token:[A-z0-9]{64}}" in a URL with provided the parameter.
func DropURLRegex(URLPath, param string) string {
	r := regexp.MustCompile(`\{token:\[A-z0-9]\{64\}}`)
	return r.ReplaceAllLiteralString(URLPath, param)
}

{'fraction_non_alphanumeric': 0.12768319763138417, 'fraction_numerical': 0.01147298297557365, 'mean_word_length': 4.052336448598131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2116015', 'n_tokens_mistral': 940, 'n_tokens_neox': 898, 'n_words': 289}