github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/soap.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/xml"
     5  	"errors"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"strings"
     9  )
    10  
    11  type SoapError struct {
    12  	FaultCode   string `xml:"Body>Fault>faultcode"`
    13  	FaultString string `xml:"Body>Fault>faultstring"`
    14  }
    15  
    16  type Soap struct {
    17  	AccessToken string
    18  	Endpoint    string
    19  	Header      string
    20  	Namespace   string
    21  }
    22  
    23  func NewSoap(endpoint, namespace, accessToken string) (s *Soap) {
    24  	s = new(Soap)
    25  	s.AccessToken = accessToken
    26  	s.Namespace = namespace
    27  	s.Endpoint = endpoint
    28  	return
    29  }
    30  func (s *Soap) ExecuteLogin(username, password string) (response []byte, err error) {
    31  	soap := `
    32  		<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    33    				xmlns:urn="urn:partner.soap.sforce.com">
    34    			<soapenv:Body>
    35      			<urn:login>
    36        				<urn:username>%s</urn:username>
    37        				<urn:password>%s</urn:password>
    38      			</urn:login>
    39    			</soapenv:Body>
    40  		</soapenv:Envelope>
    41  		`
    42  	rbody := fmt.Sprintf(soap, username, password)
    43  
    44  	req, err := httpRequest("POST", s.Endpoint, strings.NewReader(rbody))
    45  	if err != nil {
    46  		return
    47  	}
    48  	req.Header.Add("Content-Type", "text/xml")
    49  	req.Header.Add("SOAPACtion", "login")
    50  
    51  	res, err := httpClient().Do(req)
    52  	if err != nil {
    53  		fmt.Println(err)
    54  		return
    55  	}
    56  	defer res.Body.Close()
    57  	if res.StatusCode == 401 {
    58  		err = errors.New("authorization expired, please run `force login`")
    59  		return
    60  	}
    61  	response, err = ioutil.ReadAll(res.Body)
    62  	if err != nil {
    63  		return
    64  	}
    65  	err = processError(response)
    66  	return
    67  
    68  }
    69  
    70  // Execute soap
    71  /*func (s *Soap) ExecuteLogin(username, password string) (response []byte, err error) {
    72  	soap := `
    73  		<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    74    				xmlns:urn="urn:partner.soap.sforce.com">
    75    			<soapenv:Body>
    76      			<urn:login>
    77        				<urn:username>%s</urn:username>
    78        				<urn:password>%s</urn:password>
    79      			</urn:login>
    80    			</soapenv:Body>
    81  		</soapenv:Envelope>
    82  		`
    83  	rbody := fmt.Sprintf(soap, username, password)
    84  
    85  	req, err := httpRequest("POST", s.Endpoint, strings.NewReader(rbody))
    86  	if err != nil {
    87  		return
    88  	}
    89  	req.Header.Add("Content-Type", "text/xml")
    90  	req.Header.Add("SOAPACtion", "login")
    91  
    92  	res, err := httpClient().Do(req)
    93  	if err != nil {
    94  		fmt.Println(err)
    95  		return
    96  	}
    97  	defer res.Body.Close()
    98  	if res.StatusCode == 401 {
    99  		err = errors.New("authorization expired, please run `force login`")
   100  		return
   101  	}
   102  	response, err = ioutil.ReadAll(res.Body)
   103  	if err != nil {
   104  		return
   105  	}
   106  	err = processError(response)
   107  	return
   108  
   109  }*/
   110  
   111  func (s *Soap) Execute(action, query string) (response []byte, err error) {
   112  	soap := `
   113  		<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   114  		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   115  		xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
   116  		xmlns:cmd="%s" 
   117  		xmlns:apex="http://soap.sforce.com/2006/08/apex">
   118  			<env:Header>
   119  				<cmd:SessionHeader>
   120  					<cmd:sessionId>%s</cmd:sessionId>
   121  				</cmd:SessionHeader>
   122  				%s
   123  			</env:Header>
   124  			<env:Body>
   125  				<%s xmlns="%s">
   126  					%s
   127  				</%s>
   128  			</env:Body>
   129  		</env:Envelope>
   130  	`
   131  	rbody := fmt.Sprintf(soap, s.Namespace,
   132  		s.AccessToken, s.Header, action, s.Namespace, query, action)
   133  	//fmt.Println(rbody)
   134  	req, err := httpRequest("POST", s.Endpoint, strings.NewReader(rbody))
   135  	if err != nil {
   136  		return
   137  	}
   138  	req.Header.Add("Content-Type", "text/xml")
   139  	req.Header.Add("SOAPACtion", action)
   140  	res, err := httpClient().Do(req)
   141  	if err != nil {
   142  		return
   143  	}
   144  	defer res.Body.Close()
   145  	if res.StatusCode == 401 {
   146  		err = errors.New("authorization expired, please run `force login`")
   147  		return
   148  	}
   149  	response, err = ioutil.ReadAll(res.Body)
   150  	if err != nil {
   151  		return
   152  	}
   153  	err = processError(response)
   154  	return
   155  }
   156  
   157  func processError(body []byte) (err error) {
   158  	var soapError SoapError
   159  	xml.Unmarshal(body, &soapError)
   160  	if soapError.FaultCode != "" {
   161  		return errors.New(soapError.FaultString)
   162  	}
   163  	return
   164  }