package httpx

import (
	"errors"
	"net/http"
	"strconv"
)

var (
	ErrQueryKeyNotFound = errors.New("query key not found")
)

func Query(r *http.Request, key string) (string, error) {
	values, ok := r.URL.Query()[key]
	if !ok || len(values) == 0 {
		return "", ErrQueryKeyNotFound
	}

	return values[0], nil
}

func QueryInt(r *http.Request, key string) (int, error) {
	value, err := Query(r, key)
	if err != nil {
		return 0, err
	}

	return strconv.Atoi(value)
}
