<commit_before>// Copyright 2015 OpsGenie. All rights reserved.
// Use of this source code is governed by a Apache Software 
// license that can be found in the LICENSE file.

/*
Package 'command' creates various OpsGenie API clients:
 	- Alert
 	- Heartbeat
 	- Integration
 	- Policy
*/
package command

import (
	ogcli "github.com/opsgenie/opsgenie-go-sdk/client" 
	"errors"
	gcfg "code.google.com/p/gcfg"
	"encoding/json"
	yaml "gopkg.in/yaml.v2"
	gcli "github.com/codegangsta/cli"
	"os"
	log "gopkg.in/inconshreveable/log15.v2"
	"fmt"
)

var cmdlog = log.New("opsgenie", "lamp")

// The configuration file used by the client
const CONF_FILE string = "/conf/opsgenie-integration.conf"

// Configuration is parsed from an 'ini' style file.
// The key-value pairs are stored inside a struct data type.
// TODO logging properties to be read
type LampConfig struct {
	Lamp struct {
		ApiKey string
	}
	Proxy struct {
		Enabled		bool
		Username 	string
		Password 	string
		Host 		string
		Port 		int
		Secured 	bool
	}
	Logging struct{
		Enabled		bool
		Level 		string
		File 		string
		Format 		string
	}
}

var lampCfg LampConfig
// The 'Api key' is the most common parameter for all commands.
// It is provided either on command line or on the configuration file.
// The 'grabApiKey' function is used through all commands in purpose of
// creating OpsGenie clients.
func grabApiKey(c *gcli.Context) string {
	if c.IsSet("apiKey") {
		return c.String("apiKey")
	} else {
		return lampCfg.Lamp.ApiKey
	}
	return ""
}

func getProxyConf() (proxy *ogcli.ClientProxyConfiguration) {
	pc := new (ogcli.ClientProxyConfiguration)
	pc.Host = lampCfg.Proxy.Host
	pc.Port = lampCfg.Proxy.Port
	if lampCfg.Proxy.Username != "" && lampCfg.Proxy.Password != "" {
		pc.Username = lampCfg.Proxy.Username
		pc.Password = <PASSWORD>
	}
	pc.Secured = lampCfg.Proxy.Secured
	return pc
}


// In order to interact with the Alert API, one must handle an AlertClient.
// The 'NewAlertClient' function creates and returns an instance of that type.
func NewAlertClient(apiKey string) (*ogcli.OpsGenieAlertClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}
	alertCli, cliErr := cli.Alert()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the alert client")
	}	
	return alertCli, nil
}
// In order to interact with the Heartbeat API, one must handle a HeartbeatClient.
// The 'NewHeartbeatClient' function creates and returns an instance of that type.
func NewHeartbeatClient(apiKey string) (*ogcli.OpsGenieHeartbeatClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	hbCli, cliErr := cli.Heartbeat()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the heartbeat client")
	}	
	return hbCli, nil
}
// In order to interact with the Integration API, one must handle an IntegrationClient.
// The 'NewIntegrationClient' function creates and returns an instance of that type.
func NewIntegrationClient(apiKey string) (*ogcli.OpsGenieIntegrationClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	intCli, cliErr := cli.Integration()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the integration client")
	}	
	return intCli, nil
}
// In order to interact with the Policy API, one must handle a PolicyClient.
// The 'NewPolicyClient' function creates and returns an instance of that type.
func NewPolicyClient(apiKey string) (*ogcli.OpsGeniePolicyClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	polCli, cliErr := cli.Policy()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the policy client")
	}	
	return polCli, nil<commit_msg>connection timeout and request max retry features added<commit_after>// Copyright 2015 OpsGenie. All rights reserved.
// Use of this source code is governed by a Apache Software 
// license that can be found in the LICENSE file.

/*
Package 'command' creates various OpsGenie API clients:
 	- Alert
 	- Heartbeat
 	- Integration
 	- Policy
*/
package command

import (
	ogcli "github.com/opsgenie/opsgenie-go-sdk/client" 
	"errors"
	gcfg "code.google.com/p/gcfg"
	"encoding/json"
	yaml "gopkg.in/yaml.v2"
	gcli "github.com/codegangsta/cli"
	"os"
	log "gopkg.in/inconshreveable/log15.v2"
	"fmt"
	"time"
)

var cmdlog = log.New("opsgenie", "lamp")

// The configuration file used by the client
const CONF_FILE string = "/conf/opsgenie-integration.conf"

// Configuration is parsed from an 'ini' style file.
// The key-value pairs are stored inside a struct data type.
// TODO logging properties to be read
type LampConfig struct {
	Lamp struct {
		ApiKey string
	}
	Proxy struct {
		Enabled		bool
		Username 	string
		Password 	string
		Host 		string
		Port 		int
		Secured 	bool
	}
	Logging struct {
		Enabled		bool
		Level 		string
		File 		string
		Format 		string
	}
	Connection struct {
		Usedefaults 	bool
		Timeout			time.Duration
		Retries 		int
	}
}

var lampCfg LampConfig
// The 'Api key' is the most common parameter for all commands.
// It is provided either on command line or on the configuration file.
// The 'grabApiKey' function is used through all commands in purpose of
// creating OpsGenie clients.
func grabApiKey(c *gcli.Context) string {
	if c.IsSet("apiKey") {
		return c.String("apiKey")
	} else {
		return lampCfg.Lamp.ApiKey
	}
	return ""
}

func getProxyConf() (proxy *ogcli.ClientProxyConfiguration) {
	pc := new (ogcli.ClientProxyConfiguration)
	pc.Host = lampCfg.Proxy.Host
	pc.Port = lampCfg.Proxy.Port
	if lampCfg.Proxy.Username != "" && lampCfg.Proxy.Password != "" {
		pc.Username = lampCfg.Proxy.Username
		pc.Password = lampCfg.Proxy.Password
	}
	pc.Secured = lampCfg.Proxy.Secured
	return pc
}

func getConnectionConf() (connCfg *ogcli.HttpTransportSettings) {
	cfg := new (ogcli.HttpTransportSettings)
	cfg.ConnectionTimeout = lampCfg.Connection.Timeout
	cfg.MaxRetryAttempts = lampCfg.Connection.Retries
	return cfg
}

// In order to interact with the Alert API, one must handle an AlertClient.
// The 'NewAlertClient' function creates and returns an instance of that type.
func NewAlertClient(apiKey string) (*ogcli.OpsGenieAlertClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}
	if !lampCfg.Connection.Usedefaults {
		cli.SetHttpTransportSettings( getConnectionConf() )
	}
	alertCli, cliErr := cli.Alert()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the alert client")
	}	
	return alertCli, nil
}
// In order to interact with the Heartbeat API, one must handle a HeartbeatClient.
// The 'NewHeartbeatClient' function creates and returns an instance of that type.
func NewHeartbeatClient(apiKey string) (*ogcli.OpsGenieHeartbeatClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	if !lampCfg.Connection.Usedefaults {
		cli.SetHttpTransportSettings( getConnectionConf() )
	}
	hbCli, cliErr := cli.Heartbeat()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the heartbeat client")
	}	
	return hbCli, nil
}
// In order to interact with the Integration API, one must handle an IntegrationClient.
// The 'NewIntegrationClient' function creates and returns an instance of that type.
func NewIntegrationClient(apiKey string) (*ogcli.OpsGenieIntegrationClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	if !lampCfg.Connection.Usedefaults {
		cli.SetHttpTransportSettings( getConnectionConf() )
	}
	intCli, cliErr := cli.Integration()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the integration client")
	}	
	return intCli, nil
}
// In order to interact with the Policy API, one must handle a PolicyClient.
// The 'NewPolicyClient' function creates and returns an instance of that type.
func NewPolicyClient(apiKey string) (*ogcli.OpsGeniePolicyClient, error) {
	cli := new (ogcli.OpsGenieClient)
	cli.SetApiKey(apiKey)
	if lampCfg.Proxy.Enabled {
		cli.SetClientProxyConfiguration( getProxyConf() )
	}	
	if !lampCfg.Connection.Usedefaults {
		cli.SetHttpTransportSettings( getConnectionConf() )
	}
	polCli, cliErr := cli.Policy()
	
	if cliErr != nil {
		return nil, errors.New("Can not create the policy client")
	}	
	return polCli, nil
{'fraction_non_alphanumeric': 0.09816933638443937, 'fraction_numerical': 0.0018306636155606408, 'mean_word_length': 4.038040345821326, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1310223', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2848, 'n_words': 874}