Skip to content

Commit

Permalink
Separate generic http into it's own pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Apr 26, 2018
1 parent 8f1bfdf commit 74111fb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
7 changes: 5 additions & 2 deletions airflow/airflow.go
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/astronomerio/astro-cli/airflow/include"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/pkg/fileutil"
"github.com/astronomerio/astro-cli/pkg/httputil"
)

var (
HTTP = httputil.NewHTTPClient()
)

func initDirs(root string, dirs []string) bool {
Expand Down Expand Up @@ -83,7 +88,6 @@ func Init(path string) error {

// Create new airflow deployment
func Create(title string) error {
HTTP := houston.NewHTTPClient()
API := houston.NewHoustonClient(HTTP)

body, houstonErr := API.CreateDeployment(title)
Expand All @@ -97,7 +101,6 @@ func Create(title string) error {

// List all airflow deployments
func List() error {
HTTP := houston.NewHTTPClient()
API := houston.NewHoustonClient(HTTP)

body, houstonErr := API.FetchDeployments()
Expand Down
6 changes: 5 additions & 1 deletion auth/auth.go
Expand Up @@ -6,16 +6,20 @@ import (
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/docker"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/input"
)

var (
HTTP = httputil.NewHTTPClient()
)

// Login logs a user into the docker registry. Will need to login to Houston next.
func Login() {
registry := config.CFG.RegistryAuthority.GetString()
username := input.InputText("Username: ")
password, _ := input.InputPassword("Password: ")

HTTP := houston.NewHTTPClient()
API := houston.NewHoustonClient(HTTP)

// authenticate with houston
Expand Down
38 changes: 19 additions & 19 deletions houston/houston.go
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/pkg/errors"
// "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -57,30 +57,27 @@ var (
// log = logrus.WithField("package", "houston")
)

// Client containers the logger and HTTPClient used to communicate with the HoustonAPI
type Client struct {
HTTPClient *HTTPClient
}

type HoustonResponse struct {
Raw *http.Response
Body string
// HoustonClient containers the logger and HTTPClient used to communicate with the HoustonAPI
type HoustonClient struct {
HTTPClient *httputil.HTTPClient
}

// NewHoustonClient returns a new Client with the logger and HTTP client setup.
func NewHoustonClient(HTTPClient *HTTPClient) *Client {
return &Client{
HTTPClient: HTTPClient,
func NewHoustonClient(c *httputil.HTTPClient) *HoustonClient {
return &HoustonClient{
HTTPClient: c,
}
}

// GraphQLQuery wraps a graphql query string
type GraphQLQuery struct {
Query string `json:"query"`
}

func (c *Client) QueryHouston(query string) (HoustonResponse, error) {
// QueryHouston executes a query against the Houston API
func (c *HoustonClient) QueryHouston(query string) (httputil.HTTPResponse, error) {
// logger := log.WithField("function", "QueryHouston")
doOpts := DoOptions{
doOpts := httputil.DoOptions{
Data: GraphQLQuery{query},
Headers: map[string]string{
"Accept": "application/json",
Expand All @@ -96,7 +93,7 @@ func (c *Client) QueryHouston(query string) (HoustonResponse, error) {
// doOpts.Headers["organization"] = config.GetString(config.OrgIDCFG)
// }

var response HoustonResponse
var response httputil.HTTPResponse
httpResponse, err := c.HTTPClient.Do("POST", config.APIURL(), &doOpts)
if err != nil {
return response, err
Expand All @@ -109,7 +106,10 @@ func (c *Client) QueryHouston(query string) (HoustonResponse, error) {
return response, err
}

response = HoustonResponse{httpResponse, string(body)}
response = httputil.HTTPResponse{
Raw: httpResponse,
Body: string(body),
}

// logger.Debug(query)
// logger.Debug(response.Body)
Expand All @@ -119,7 +119,7 @@ func (c *Client) QueryHouston(query string) (HoustonResponse, error) {

// CreateDeployment will send request to Houston to create a new AirflowDeployment
// Returns a CreateDeploymentResponse which contains the unique id of deployment
func (c *Client) CreateDeployment(title string) (*CreateDeploymentResponse, error) {
func (c *HoustonClient) CreateDeployment(title string) (*CreateDeploymentResponse, error) {
// logger := log.WithField("method", "CreateDeployment")
// logger.Debug("Entered CreateDeployment")

Expand All @@ -142,7 +142,7 @@ func (c *Client) CreateDeployment(title string) (*CreateDeploymentResponse, erro

// CreateToken will request a new token from Houston, passing the users e-mail and password.
// Returns a CreateTokenResponse structure with the users ID and Token inside.
func (c *Client) CreateToken(email string, password string) (*CreateTokenResponse, error) {
func (c *HoustonClient) CreateToken(email string, password string) (*CreateTokenResponse, error) {
// logger := log.WithField("method", "CreateToken")
// logger.Debug("Entered CreateToken")

Expand All @@ -165,7 +165,7 @@ func (c *Client) CreateToken(email string, password string) (*CreateTokenRespons

// FetchDeployments will request all airflow deployments from Houston
// Returns a FetchDeploymentResponse structure with deployment details
func (c *Client) FetchDeployments() (*FetchDeploymentsResponse, error) {
func (c *HoustonClient) FetchDeployments() (*FetchDeploymentsResponse, error) {
// logger := log.WithField("method", "FetchDeployments")
// logger.Debug("Entered FetchDeployments")

Expand Down
8 changes: 7 additions & 1 deletion houston/http.go → pkg/httputil/http.go
@@ -1,4 +1,4 @@
package houston
package httputil

import (
"bytes"
Expand All @@ -18,6 +18,12 @@ type HTTPClient struct {
HTTPClient *http.Client
}

// HTTPResponse houses respnse object
type HTTPResponse struct {
Raw *http.Response
Body string
}

// DoOptions are options passed to the HTTPClient.Do function
type DoOptions struct {
Data interface{}
Expand Down

0 comments on commit 74111fb

Please sign in to comment.