You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cookies_middleware.go
    path of file: ./repos/grafana/pkg/services/pluginsintegration/clientmiddleware
    
    the code of the file until where you have to start completion: package clientmiddleware

import (
	"context"

	"github.com/grafana/grafana-plugin-sdk-go/backend"
	"github.com/grafana/grafana/pkg/components/simplejson"
	"github.com/grafana/grafana/pkg/plugins"
	"github.com/grafana/grafana/pkg/services/contexthandler"
	"github.com/grafana/grafana/pkg/services/datasources"
	"github.com/grafana/grafana/pkg/
    
func (m *CookiesMiddleware) applyCookies(ctx context.Context, pCtx backend.PluginContext, req any) error {
	reqCtx := contexthandler.FromContext(ctx)
	// if request not for a datasource or no HTTP request context skip middleware
	if req == nil || pCtx.DataSourceInstanceSettings == nil || reqCtx == nil || reqCtx.Req == nil {
		return nil
	}

	settings := pCtx.DataSourceInstanceSettings
	jsonDataBytes, err := simplejson.NewJson(settings.JSONData)
	if err != nil {
		return err
	}

	ds := &datasources.DataSource{
		ID:       settings.ID,
		OrgID:    pCtx.OrgID,
		JsonData: jsonDataBytes,
		Updated:  settings.Updated,
	}

	proxyutil.ClearCookieHeader(reqCtx.Req, ds.AllowedCookies(), m.skipCookiesNames)

	cookieStr := reqCtx.Req.Header.Get(cookieHeaderName)
	switch t := req.(type) {
	case *backend.QueryDataRequest:
		if cookieStr == "" {
			delete(t.Headers, cookieHeaderName)
		} else {
			t.Headers[cookieHeaderName] = cookieStr
		}
	case *backend.CheckHealthRequest:
		if cookieStr == "" {
			delete(t.Headers, cookieHeaderName)
		} else {
			t.Headers[cookieHeaderName] = cookieStr
		}
	case *backend.CallResourceRequest:
		if cookieStr == "" {
			delete(t.Headers, cookieHeaderName)
		} else {
			t.Headers[cookieHeaderName] = []string{cookieStr}
		}
	}

	return nil
}