(message string)
| 92 | } |
| 93 | |
| 94 | func (p *PsyNetRPC) parseMessage(message string) (*PsyResponse, error) { |
| 95 | delimiter := "\r\n\r\n" |
| 96 | index := strings.Index(message, delimiter) |
| 97 | if index == -1 { |
| 98 | return nil, fmt.Errorf("message does not contain expected delimiter") |
| 99 | } |
| 100 | |
| 101 | headersPart := message[:index] |
| 102 | jsonPayload := message[index+len(delimiter):] |
| 103 | |
| 104 | headers := make(map[string]string) |
| 105 | headerLines := strings.Split(headersPart, "\r\n") |
| 106 | for _, line := range headerLines { |
| 107 | if colonIndex := strings.Index(line, ":"); colonIndex != -1 { |
| 108 | key := strings.TrimSpace(line[:colonIndex]) |
| 109 | value := strings.TrimSpace(line[colonIndex+1:]) |
| 110 | headers[key] = value |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | responseID := headers["PsyResponseID"] |
| 115 | |
| 116 | var jsonResult PsyResponse |
| 117 | if err := json.Unmarshal([]byte(jsonPayload), &jsonResult); err != nil { |
| 118 | return nil, fmt.Errorf("failed to parse json payload: %w", err) |
| 119 | } |
| 120 | |
| 121 | jsonResult.ResponseID = responseID |
| 122 | |
| 123 | return &jsonResult, nil |
| 124 | } |
| 125 | |
| 126 | func (p *PsyNetRPC) buildMessage(headers map[string]string, body interface{}) (string, error) { |
| 127 | var message strings.Builder |
no outgoing calls