Skip to content

Commit

Permalink
Moved http Client into struct.
Browse files Browse the repository at this point in the history
Formatted url creation
  • Loading branch information
Whyeasy committed Jun 24, 2020
1 parent 25bc0ff commit cd4818b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
12 changes: 4 additions & 8 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,25 @@ type MeasurementsStats struct {

//ExporterClient contains SonarCloud information for connecting
type ExporterClient struct {
Token string
Organization string
sqc *sonar.Client
}

//New returns a new Client connection to SonarCloud
func New(c internal.Config) *ExporterClient {
return &ExporterClient{
Token: c.Token,
Organization: c.Organization,
sqc: sonar.NewClient(c.Token, c.Organization),
}
}

//GetStats retrieves data from API to create metrics from.
func (c *ExporterClient) GetStats() (*Stats, error) {

sqc := sonar.NewClient(c.Token, c.Organization)

projects, err := getProjects(sqc)
projects, err := getProjects(c.sqc)
if err != nil {
return nil, err
}

measurements, err := getMeasurements(sqc, projects)
measurements, err := getMeasurements(c.sqc, projects)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion lib/sonar/measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sonar

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
Expand All @@ -24,7 +25,8 @@ type MeasurementResponse struct {

//ProjectMeasurements retrieves the measurements we pass through about the projects.
func (c *Client) ProjectMeasurements(key string) (*MeasurementResponse, error) {
url := c.sonarConnectionString + "/measures/component?metricKeys=ncloc,coverage,vulnerabilities,bugs,violations&component=" + key

url := fmt.Sprintf("%s/measures/component?metricKeys=ncloc,coverage,vulnerabilities,bugs,violations&component=%s", c.sonarConnectionString, key)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion lib/sonar/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sonar

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
Expand All @@ -28,7 +29,7 @@ type ProjectResponse struct {
//ListProjects lists the current projects in the organization.
func (c *Client) ListProjects(opt *ListOptions) (*ProjectResponse, error) {

url := c.sonarConnectionString + "/projects/search?organization=" + c.organization + "&p=" + strconv.Itoa(opt.Page)
url := fmt.Sprintf("%s/projects/search?organization=%s&p=%s", c.sonarConnectionString, c.organization, strconv.Itoa(opt.Page))

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down

0 comments on commit cd4818b

Please sign in to comment.