reichelt/connection.go

42 lines
713 B
Go
Raw Normal View History

2017-03-12 14:47:59 +01:00
package reichelt
import (
"fmt"
"net/http"
"net/http/cookiejar"
)
type Connection struct {
client http.Client
}
2017-03-31 14:43:47 +02:00
// Opens a new connection to the reichelt-Server
// this will try to connect and consequently
// throw an error on connection failure
2017-03-12 14:47:59 +01:00
func NewConnection() (c *Connection, err error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
c = &Connection{
client: http.Client{
Jar: jar,
},
}
2017-03-31 14:43:47 +02:00
// set reichelt SID cookie
2017-03-12 14:47:59 +01:00
resp, err := c.client.Get(apiurl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Wrong Status response: %d(%s)", resp.StatusCode, resp.Status)
}
return c, nil
}