<commit_before>package api

// Repository defines the behavior of a proxy specs repository
type Repository interface {
	FindAll() ([]*Definition, error)
	FindByName(name string) (*Definition, error)
	FindByListenPath(path string) (*Definition, error)
	Exists(def *Definition) (bool, error)
	Add(app *Definition) error
	Remove(name string) error
	FindValidAPIHealthChecks() ([]*Definition, error)
}

func exists(r Repository, def *Definition) (bool, error) {
	_, err := r.FindByName(def.Name)
	if nil != err && err != ErrAPIDefinitionNotFound {
		return false, err
	} else if err != ErrAPIDefinitionNotFound {
		return true, ErrAPINameExists
	}

	_, err = r.FindByListenPath(def.Proxy.ListenPath)
	if nil != err && err != ErrAPIDefinitionNotFound {
		return false, err
	} else if err != ErrAPIDefinitionNotFound {
		return true, ErrAPIListenPathExists
	}

	return false, nil
}<commit_msg>Added a builder for the repository<commit_after>package api

import (
	"net/url"

	mgo "gopkg.in/mgo.v2"

	"github.com/containous/traefik/log"
	"github.com/pkg/errors"
)

// Repository defines the behavior of a proxy specs repository
type Repository interface {
	FindAll() ([]*Definition, error)
	FindByName(name string) (*Definition, error)
	FindByListenPath(path string) (*Definition, error)
	Exists(def *Definition) (bool, error)
	Add(app *Definition) error
	Remove(name string) error
	FindValidAPIHealthChecks() ([]*Definition, error)
}

func exists(r Repository, def *Definition) (bool, error) {
	_, err := r.FindByName(def.Name)
	if nil != err && err != ErrAPIDefinitionNotFound {
		return false, err
	} else if err != ErrAPIDefinitionNotFound {
		return true, ErrAPINameExists
	}

	_, err = r.FindByListenPath(def.Proxy.ListenPath)
	if nil != err && err != ErrAPIDefinitionNotFound {
		return false, err
	} else if err != ErrAPIDefinitionNotFound {
		return true, ErrAPIListenPathExists
	}

	return false, nil
}

// BuildRepository creates a repository instance that will depend on your given DSN
func BuildRepository(dsn string, session *mgo.Session) (Repository, error) {
	var repo Repository
	dsnURL, err := url.Parse(dsn)
	switch dsnURL.Scheme {
	case "mongodb":
		repo, err = NewMongoAppRepository(session)
		if err != nil {
			return nil, errors.Wrap(err, "Could not create a mongodb repository for api definitions")
		}
	case "file":
		log.Debug("File system based configuration chosen")
		var apiPath = dsnURL.Path + "/apis"

		log.WithField("api_path", apiPath).Debug("Trying to load configuration files")
		repo, err = NewFileSystemRepository(apiPath)
		if err != nil {
			return nil, errors.Wrap(err, "could not create a file system repository")
		}
	default:
		return nil, errors.New("The selected scheme is not supported to load API definitions")
	}

	return repo, nil
}
{'fraction_non_alphanumeric': 0.1094706517824991, 'fraction_numerical': 0.00036010082823190496, 'mean_word_length': 4.836134453781512, 'pattern_counts': {'":': 2, '<': 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': '6053035', 'n_tokens_mistral': 906, 'n_tokens_neox': 870, 'n_words': 271}