<commit_before>package cfwrapper

import (
	"net/url"

	cfclient "github.com/venicegeo/go-cfclient"
	"github.com/venicegeo/pzsvc-exec/pzsvc"
)

// CFSession is an abstraction around the Go CF client library to make its inclusion modular
type CFSession interface {
	IsValid() (bool, error)
	CountTasksForApp(appID string) (int, error)
	CreateTask(request TaskRequest) error
}

// CFWrappedSession is an implementation of CFSession that wraps an actual cfclient.Client
type wrappedCFSession struct {
	PzSession *pzsvc.Session
	Client    *cfclient.Client
}

// IsValid checks if the session's client is live and useful
func (s wrappedCFSession) IsValid() (bool, error) {
	if s.Client == nil {
		return false, nil
	}

	//Configure a query that will filter out everything.
	validationQueryParams := url.Values{}
	validationQueryParams.Add("q", "name:dummy_name")

	pzsvc.LogInfo(*s.PzSession, "Submitting client validation request.")
	_, err := s.Client.ListAppsByQuery(validationQueryParams)
	pzsvc.LogInfo(*s.PzSession, "Validation request complete.")
	if err == nil {
		return true, nil
	}

	if cfclient.IsNotAuthorizedError(err) || cfclient.IsNotAuthenticatedError(err) {
		return false, nil
	}
	return false, err
}

// CountTasksForApp returns the number of currently running tasks for a CF app
func (s wrappedCFSession) CountTasksForApp(appID string) (int, error) {
	query := url.Values{}
	query.Add("states", "RUNNING")
	tasks, err := s.Client.TasksByAppByQuery(appID, query)<commit_msg>Swapped to a cleaner place-holder name.<commit_after>package cfwrapper

import (
	"net/url"

	cfclient "github.com/venicegeo/go-cfclient"
	"github.com/venicegeo/pzsvc-exec/pzsvc"
)

// CFSession is an abstraction around the Go CF client library to make its inclusion modular
type CFSession interface {
	IsValid() (bool, error)
	CountTasksForApp(appID string) (int, error)
	CreateTask(request TaskRequest) error
}

// CFWrappedSession is an implementation of CFSession that wraps an actual cfclient.Client
type wrappedCFSession struct {
	PzSession *pzsvc.Session
	Client    *cfclient.Client
}

// IsValid checks if the session's client is live and useful
func (s wrappedCFSession) IsValid() (bool, error) {
	if s.Client == nil {
		return false, nil
	}

	//Configure a query that will filter out everything.
	validationQueryParams := url.Values{}
	validationQueryParams.Add("q", "name:ndwi")

	pzsvc.LogInfo(*s.PzSession, "Submitting client validation request.")
	_, err := s.Client.ListAppsByQuery(validationQueryParams)
	pzsvc.LogInfo(*s.PzSession, "Validation request complete.")
	if err == nil {
		return true, nil
	}

	if cfclient.IsNotAuthorizedError(err) || cfclient.IsNotAuthenticatedError(err) {
		return false, nil
	}
	return false, err
}

// CountTasksForApp returns the number of currently running tasks for a CF app
func (s wrappedCFSession) CountTasksForApp(appID string) (int, error) {
	query := url.Values{}
	query.Add("states", "RUNNING")
	tasks, err := s.Client.TasksByAppByQuery(appID, query)
{'fraction_non_alphanumeric': 0.10340226817878585, 'fraction_numerical': 0.0, 'mean_word_length': 4.926877470355731, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6695471', 'n_tokens_mistral': 987, 'n_tokens_neox': 964, 'n_words': 284}