<commit_before>package hateoas

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func Handle(apiPath string, rh ResourceHandler) error {
	var err error

	location := fmt.Sprint(apiPath, "/", rh.ResourceName())
	http.HandleFunc(location, errorWrapper)

	return err
}

func errorWrapper(w http.ResponseWriter, r *http.Request) {
	var err *Error

	err = handle(w, r)

	if err != nil {
		w.WriteHeader(err.Status)

		errJson, _ := json.MarshalIndent(err, "", "    ")
		fmt.Fprint(w, string(errJson))
	}
}

func handle(w http.ResponseWriter, r *http.Request) *Error {
	var err *Error

	err = &Error{}
	err.Status = 500
	err.Code = 1
	err.Message = "Under construction. Please check again later."
	err.DeveloperMessage = "API is not ready yet. Please contact <EMAIL> for more information."

	return err
}<commit_msg>Add support for path parameter (ID) when requesting a resource<commit_after>package hateoas

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
)

func Handle(apiPath string, rh ResourceHandler) error {
	var err error

	location := fmt.Sprint(apiPath, "/", rh.ResourceName(), "/")
	rootPathLength := len(location)

	http.HandleFunc(location, errorWrapper(rootPathLength))

	return err
}

func errorWrapper(rootPathLength int) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		var err *Error

		id := r.URL.Path[rootPathLength:]

		if strings.Contains(id, "/") {
			err = &Error{}
			err.Status = 500
			err.Code = 1
			err.Message = "Invalid request. Resource could not be fetched."
			err.DeveloperMessage = "Request for resource is incorrect. More than one path parameters (ID) is not supported for a resource."
		}

		if err == nil {
			err = handle(w, r, id)
		}

		if err != nil {
			w.WriteHeader(err.Status)

			errJson, _ := json.MarshalIndent(err, "", "    ")
			fmt.Fprint(w, string(errJson))
		}
	}
}

func handle(w http.ResponseWriter, r *http.Request, id string) *Error {
	var err *Error

	err = &Error{}
	err.Status = 500
	err.Code = 1
	err.Message = "Under construction. Please check again later."
	err.DeveloperMessage = "API is not ready yet. Please contact <EMAIL> for more information."

	switch r.Method {
	case "GET":
		fmt.Println("GET request")
		break

	case "POST":
		fmt.Println("POST request")
		break

	case "PUT":
		fmt.Println("PUT request")
		break

	case "DELETE":
		fmt.Println("DELETE request")
		break
	}

	if id == "" {
		fmt.Println("No resource ID found")
	} else {
		fmt.Println("Resource ID requested", id)
	}

	return err
}
{'fraction_non_alphanumeric': 0.13191153238546605, 'fraction_numerical': 0.004739336492890996, 'mean_word_length': 3.7345794392523364, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1908851', 'n_tokens_mistral': 933, 'n_tokens_neox': 850, 'n_words': 248}