Files
schedule_converter/client/http.go
lan accc6567f2
All checks were successful
Build / build (push) Successful in 5m28s
refactor(grpc): restructure task handling and migrate to slog
Refactor the gRPC client and task handling logic to improve modularity,
error handling, and observability.

- Split `grpc/handler.go` into specialized handler files (login,
  classroom, exams, grades, schedule) to reduce complexity.
- Replace standard `log` with `log/slog` throughout the project for
  structured logging.
- Refactor `grpc/client.go` to use a thread-safe connection management
  pattern with `sync.RWMutex`.
- Remove the legacy `service` package and HTTP server implementation,
  transitioning the application to a pure gRPC runner mode.
- Clean up `config` and `pkg/errors` to remove unused utilities and
  simplify the API.
- Improve error reporting in parsers and HTTP clients by checking
  response status codes.
2026-06-09 23:08:03 +08:00

52 lines
1.3 KiB
Go

package client
import (
"log/slog"
"net/http"
"strings"
"schedule_converter/config"
)
const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
func GetHeaders() http.Header {
return http.Header{
"User-Agent": []string{defaultUserAgent},
"Accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
"Accept-Language": []string{"zh-CN,zh;q=0.9,en;q=0.8"},
}
}
func GetHeadersWithContentType(contentType string) http.Header {
headers := GetHeaders()
headers.Set("Content-Type", contentType)
return headers
}
func GetInitialCookiesWithClient(httpClient *http.Client) error {
slog.Info("访问登录页面获取初始Cookie")
req, err := http.NewRequest("GET", config.Get().BaseURL+"/loginNOCAS", nil)
if err != nil {
return err
}
req.Header = GetHeaders()
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
slog.Info("登录页面访问完成", "status_code", resp.StatusCode)
return nil
}
func CheckLoginSuccess(body string) bool {
return strings.Contains(body, "本科教学管理服务系统")
}
func IsCaptchaError(body string) bool {
return strings.Contains(body, "验证码")
}