| 35 | } |
| 36 | |
| 37 | func testConnection(ctx context.Context, connection models.GiteeConn) (*GiteeTestConnResponse, errors.Error) { |
| 38 | // validate |
| 39 | if vld != nil { |
| 40 | if err := vld.Struct(connection); err != nil { |
| 41 | return nil, errors.Default.Wrap(err, "error validating target") |
| 42 | } |
| 43 | } |
| 44 | apiClient, err := helper.NewApiClientFromConnection(ctx, basicRes, &connection) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | res, err := apiClient.Get("user", nil, nil) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | resBody := &models.ApiUserResponse{} |
| 53 | err = helper.UnmarshalResponse(res, resBody) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | if res.StatusCode == http.StatusUnauthorized { |
| 59 | return nil, errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when testing connection") |
| 60 | } |
| 61 | |
| 62 | if res.StatusCode != http.StatusOK { |
| 63 | return nil, errors.HttpStatus(res.StatusCode).New("unexpected status code when testing connection") |
| 64 | } |
| 65 | connection = connection.Sanitize() |
| 66 | body := GiteeTestConnResponse{} |
| 67 | body.Success = true |
| 68 | body.Message = "success" |
| 69 | body.Connection = &connection |
| 70 | // output |
| 71 | return &body, nil |
| 72 | } |
| 73 | |
| 74 | // TestConnection test gitee connection |
| 75 | // @Summary test gitee connection |