github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/sdk/meta.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under 
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>. 
     9  //
    10  // https://documize.com
    11  
    12  package documize
    13  
    14  import (
    15  	"io/ioutil"
    16  	"net/http"
    17  )
    18  
    19  // GetSiteMeta returns the site information based on the sub-domain of the URL called.
    20  /* TODO - cant get sub-domain easily in test environment
    21  func (c *Client) GetSiteMeta() (*entity.SiteMeta, error) {
    22  	tgt := c.BaseURL + "/api/public/meta"
    23  	req, err := http.NewRequest("GET", tgt, nil)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
    28  	resp, err := c.Client.Do(req)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	defer resp.Body.Close() // ignore error
    33  	b, err := ioutil.ReadAll(resp.Body)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	var sm entity.SiteMeta
    38  	err = json.Unmarshal(b, &sm)
    39  	if err != nil {
    40  		return nil, errors.New(trimErrors(string(b)))
    41  	}
    42  	return &sm, nil
    43  }
    44  */
    45  
    46  // GetSitemap returns the site map information based on the domain supplied in the URL called.
    47  func (c *Client) GetSitemap() ([]byte, error) {
    48  	tgt := c.BaseURL + "/sitemap.xml"
    49  	return c.getFile(tgt)
    50  }
    51  
    52  // GetRobots returns the site map information based on the domain supplied in the URL called.
    53  func (c *Client) GetRobots() ([]byte, error) {
    54  	tgt := c.BaseURL + "/robots.txt"
    55  	return c.getFile(tgt)
    56  }
    57  
    58  // getFile is an internal function to avoid code duplication when fetching the plain output of the GET.
    59  func (c *Client) getFile(tgt string) ([]byte, error) {
    60  	req, err := http.NewRequest("GET", tgt, nil)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
    65  	resp, err := c.Client.Do(req)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	defer resp.Body.Close() // ignore error
    70  	b, err := ioutil.ReadAll(resp.Body)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return b, nil
    75  }