session.go raw
1 package internal
2
3 import (
4 "context"
5 "fmt"
6 )
7
8 type sessionKey string
9
10 const sessionIDKey sessionKey = "sessionID"
11
12 // login performs the login as specified by the netcup WSDL
13 // returns sessionID needed to perform remaining actions.
14 // https://ccp.netcup.net/run/webservice/servers/endpoint.php
15 func (c *Client) login(ctx context.Context) (string, error) {
16 payload := &Request{
17 Action: "login",
18 Param: &LoginRequest{
19 CustomerNumber: c.customerNumber,
20 APIKey: c.apiKey,
21 APIPassword: c.apiPassword,
22 ClientRequestID: "",
23 },
24 }
25
26 var responseData LoginResponse
27
28 err := c.doRequest(ctx, payload, &responseData)
29 if err != nil {
30 return "", fmt.Errorf("loging error: %w", err)
31 }
32
33 return responseData.APISessionID, nil
34 }
35
36 // Logout performs the logout with the supplied sessionID as specified by the netcup WSDL.
37 // https://ccp.netcup.net/run/webservice/servers/endpoint.php
38 func (c *Client) Logout(ctx context.Context) error {
39 payload := &Request{
40 Action: "logout",
41 Param: &LogoutRequest{
42 CustomerNumber: c.customerNumber,
43 APIKey: c.apiKey,
44 APISessionID: getSessionID(ctx),
45 ClientRequestID: "",
46 },
47 }
48
49 err := c.doRequest(ctx, payload, nil)
50 if err != nil {
51 return fmt.Errorf("logout error: %w", err)
52 }
53
54 return nil
55 }
56
57 func (c *Client) CreateSessionContext(ctx context.Context) (context.Context, error) {
58 sessID, err := c.login(ctx)
59 if err != nil {
60 return nil, err
61 }
62
63 return context.WithValue(ctx, sessionIDKey, sessID), nil
64 }
65
66 func getSessionID(ctx context.Context) string {
67 sessID, ok := ctx.Value(sessionIDKey).(string)
68 if !ok {
69 return ""
70 }
71
72 return sessID
73 }
74