// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package mydp import ( "errors" "fmt" "io" "net/http" "net/url" ) const ( // Version defines the tag and version info for this package. Version = "mydp/v1.1.0" // DatapipeProd is the production URL for the admin.mydatapipe resource DatapipeProd = "admin.mydatapipe.com" // DatapipeDev is the devel URL for the admin.mydatapipe resource DatapipeDev = "dandev.mydatapipe.com" // IPToolProd is the production URL for the IP tool resource IPToolProd = "iptool.datapipe.net" // IPToolDev is the devel URL for the IP tool resource IPToolDev = "iptool.datapipe.net" ) // Client struct represents the MyDP connection // type client for the mydp library type Client struct { username, password string URL, IPToolURL string } // New function will take user credentials or a service account, and return the // a pointer to a new mydp.Client type. func New(username, password string, staging bool) (*Client, error) { if username == "" { return nil, errors.New("username cannot be empty") } client := &Client{ username: username, password: password, URL: DatapipeProd, IPToolURL: IPToolProd, } if staging { client.URL = DatapipeDev client.IPToolURL = IPToolDev } return client, nil } func (c *Client) get(uri string, params url.Values) (*http.Response, error) { req, err := http.NewRequest(http.MethodGet, c.url(uri), nil) if err != nil { return nil, err } req.SetBasicAuth(c.username, c.password) req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", fmt.Sprintf("gorax/clients/%s", Version)) if params != nil { req.URL.RawQuery = params.Encode() } return http.DefaultClient.Do(req) } func (c *Client) post(uri string, params url.Values, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(http.MethodPost, c.url(uri), body) if err != nil { return nil, err } req.SetBasicAuth(c.username, c.password) req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", fmt.Sprintf("gorax/clients/%s", Version)) if params != nil { req.URL.RawQuery = params.Encode() } return http.DefaultClient.Do(req) } // url is a helper function to return the built XML URL for the reqest. func (c *Client) url(path string) string { return fmt.Sprintf("https://%s:%s@%s/%s", c.username, c.password, c.URL, path) } // ipToolURL is a helper function to return the built XML URL for the reqest. func (c *Client) ipToolURL(path string) string { return fmt.Sprintf("https://%s:%s@%s/%s", c.username, c.password, c.IPToolURL, path) }