(path []string, params interface{}, result interface{})
| 123 | } |
| 124 | |
| 125 | func (p *PsyNet) postJSON(path []string, params interface{}, result interface{}) error { |
| 126 | url := fmt.Sprintf("%s/%s", baseURL, strings.Join(path, "/")) |
| 127 | |
| 128 | body, err := json.Marshal(params) |
| 129 | if err != nil { |
| 130 | return fmt.Errorf("failed to marshal params: %w", err) |
| 131 | } |
| 132 | |
| 133 | p.logger.Debug("sending http request", slog.String("url", url), slog.String("body", string(body))) |
| 134 | |
| 135 | req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) |
| 136 | if err != nil { |
| 137 | return fmt.Errorf("failed to create request: %w", err) |
| 138 | } |
| 139 | |
| 140 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 141 | req.Header.Set("User-Agent", fmt.Sprintf("RL Win/%s gzip (x86_64-pc-win32) curl-7.67.0 Schannel", p.gameVersion)) |
| 142 | req.Header.Set("PsyBuildID", p.buildID) |
| 143 | req.Header.Set("PsyEnvironment", "Prod") |
| 144 | req.Header.Set("PsyRequestID", p.requestID.getID()) |
| 145 | req.Header.Set("PsySig", generatePsySig(body)) |
| 146 | |
| 147 | resp, err := p.client.Do(req) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("failed to send request: %w", err) |
| 150 | } |
| 151 | defer resp.Body.Close() |
| 152 | |
| 153 | if resp.StatusCode != http.StatusOK { |
| 154 | return fmt.Errorf("unexpected status: %s", resp.Status) |
| 155 | } |
| 156 | |
| 157 | respBytes, err := io.ReadAll(resp.Body) |
| 158 | if err != nil { |
| 159 | return fmt.Errorf("failed to read response body: %w", err) |
| 160 | } |
| 161 | |
| 162 | p.logger.Debug("received http response", slog.String("status", resp.Status), slog.String("body", string(respBytes))) |
| 163 | |
| 164 | var wrapper struct { |
| 165 | Result json.RawMessage `json:"Result"` |
| 166 | Error *psyNetError `json:"Error"` |
| 167 | } |
| 168 | if err := json.Unmarshal(respBytes, &wrapper); err != nil { |
| 169 | return fmt.Errorf("failed to unmarshal wrapper: %w", err) |
| 170 | } |
| 171 | |
| 172 | if wrapper.Error != nil { |
| 173 | return wrapper.Error |
| 174 | } |
| 175 | |
| 176 | if err := json.Unmarshal(wrapper.Result, result); err != nil { |
| 177 | return fmt.Errorf("failed to unmarshal result: %w", err) |
| 178 | } |
| 179 | |
| 180 | return nil |
| 181 | } |
no test coverage detected