Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check response code of client API requests #20

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func (e *UnexpectedResponseContentTypeError) Error() string {
return fmt.Sprintf("unexpected response content type: %s", e.ContentType)
}

type UnexpectedResponseCodeError struct {
Code int
}

func (e *UnexpectedResponseCodeError) Error() string {
return fmt.Sprintf("unexpected response status code: %d", e.Code)
}

func NewClient(user, password, totpSeed string, options ...Option) (*Client, error) {
c := Client{}
for _, option := range options {
Expand Down Expand Up @@ -167,6 +175,9 @@ func (c *Client) loginTotp(user, password, totpSeed string) error {
if err != nil {
return err
}
if resp.IsError() {
return &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
tokenResp := strings.Trim(resp.String(), "\"")
_, _, err = jwt.NewParser().ParseUnverified(tokenResp, jwt.MapClaims{})
if err != nil {
Expand Down Expand Up @@ -207,6 +218,9 @@ func (c *Client) login(user, password string) error {
if err != nil {
return err
}
if resp.IsError() {
return &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
tokenResp := strings.Trim(resp.String(), "\"")
_, _, err = jwt.NewParser().ParseUnverified(tokenResp, jwt.MapClaims{})
if err != nil {
Expand Down Expand Up @@ -244,6 +258,9 @@ func (c *Client) GetRevocationReasons() ([]models.RevocationReasonsResponse, err
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -254,7 +271,7 @@ func (c *Client) GetRevocationReasons() ([]models.RevocationReasonsResponse, err
func (c *Client) RevokeCertificate(reason models.RevocationReasonsResponse, comment string, transactionId string) error {
c.loginLock.RLock()
defer c.loginLock.RUnlock()
_, err := c.client.R().
resp, err := c.client.R().
SetHeader("Content-Type", ApplicationJson).
SetBody(map[string]interface{}{
"transactionId": transactionId,
Expand All @@ -266,6 +283,9 @@ func (c *Client) RevokeCertificate(reason models.RevocationReasonsResponse, comm
if err != nil {
return err
}
if resp.IsError() {
return &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
return nil
}

Expand All @@ -285,6 +305,9 @@ func (c *Client) CheckMatchingOrganization(domains []string) ([]models.Organizat
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -303,6 +326,9 @@ func (c *Client) GetMyTransactions() ([]models.TransactionResponse, error) {
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -322,6 +348,9 @@ func (c *Client) GetCertificate(id string) (*models.CertificateResponse, error)
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -345,6 +374,9 @@ func (c *Client) CheckDomainNames(domains []string) ([]models.DomainResponse, er
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand Down Expand Up @@ -402,6 +434,9 @@ func (c *Client) RequestCertificate(domains []string, csr string, transactionTyp
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -425,6 +460,9 @@ func (c *Client) GetPendingReviews() ([]models.ReviewResponse, error) {
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -434,7 +472,7 @@ func (c *Client) GetPendingReviews() ([]models.ReviewResponse, error) {
func (c *Client) ApproveRequest(id, message, value string) error {
c.loginLock.RLock()
defer c.loginLock.RUnlock()
_, err := c.client.R().
resp, err := c.client.R().
SetHeader("Content-Type", "multipart/form-data").
SetMultipartFormData(map[string]string{
"reviewId": id,
Expand All @@ -444,6 +482,9 @@ func (c *Client) ApproveRequest(id, message, value string) error {
"reviewValue": value,
}).
Post(BaseURL + UpdateReviewsPath)
if resp.IsError() {
return &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if err != nil {
return err
}
Expand All @@ -462,6 +503,9 @@ func (c *Client) GetOrganizations() ([]models.Organization, error) {
if err != nil {
return nil, err
}
if resp.IsError() {
return nil, &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
if !strings.Contains(resp.Header().Get("Content-Type"), ApplicationJson) {
return nil, &UnexpectedResponseContentTypeError{ContentType: resp.Header().Get("Content-Type")}
}
Expand All @@ -471,7 +515,7 @@ func (c *Client) GetOrganizations() ([]models.Organization, error) {
func (c *Client) TriggerValidation(organizatonId, email string) error {
c.loginLock.RLock()
defer c.loginLock.RUnlock()
_, err := c.client.R().
resp, err := c.client.R().
SetHeader("Content-Type", ApplicationJson).
SetBody(map[string]string{
"organizationId": organizatonId,
Expand All @@ -483,5 +527,8 @@ func (c *Client) TriggerValidation(organizatonId, email string) error {
if err != nil {
return err
}
if resp.IsError() {
return &UnexpectedResponseCodeError{Code: resp.StatusCode()}
}
return nil
}
Loading