1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package monocular 18 19 import ( 20 "errors" 21 "net/url" 22 ) 23 24 // ErrHostnameNotProvided indicates the url is missing a hostname 25 var ErrHostnameNotProvided = errors.New("no hostname provided") 26 27 // Client represents a client capable of communicating with the Monocular API. 28 type Client struct { 29 30 // The base URL for requests 31 BaseURL string 32 33 // The internal logger to use 34 Log func(string, ...interface{}) 35 } 36 37 // New creates a new client 38 func New(u string) (*Client, error) { 39 40 // Validate we have a URL 41 if err := validate(u); err != nil { 42 return nil, err 43 } 44 45 return &Client{ 46 BaseURL: u, 47 Log: nopLogger, 48 }, nil 49 } 50 51 var nopLogger = func(_ string, _ ...interface{}) {} 52 53 // Validate if the base URL for monocular is valid. 54 func validate(u string) error { 55 56 // Check if it is parsable 57 p, err := url.Parse(u) 58 if err != nil { 59 return err 60 } 61 62 // Check that a host is attached 63 if p.Hostname() == "" { 64 return ErrHostnameNotProvided 65 } 66 67 return nil 68 } 69