Skip to content

Commit

Permalink
chore: add nil check for ListOptions receivers; update useragent value (
Browse files Browse the repository at this point in the history
  • Loading branch information
vorobeyme committed Apr 12, 2024
1 parent e5ca1b1 commit 625bdff
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
11 changes: 1 addition & 10 deletions crowdin/crowdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"io"
"net/http"
"net/url"
"time"

"github.com/crowdin/crowdin-api-client-go/crowdin/model"
)

const (
baseURL = "https://api.crowdin.com/"

userAgent = "crowdin-api-client-go/0.0.1"
userAgent = "crowdin-api-client-go/0.1.0"
)

// Client is a Crowdin API client.
Expand Down Expand Up @@ -106,14 +105,6 @@ func WithHTTPClient(hc *http.Client) ClientOption {
}
}

// WithTimeout modifies the default HTTP client timeout.
func WithTimeout(timeout time.Duration) ClientOption {
return func(c *Client) error {
c.httpClient.Timeout = timeout
return nil
}
}

// RequestOption represents an option that can be used to modify a http.Request.
type RequestOption func(*http.Request) error

Expand Down
4 changes: 4 additions & 0 deletions crowdin/model/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type BranchesListOptions struct {

// Values returns the url.Values representation of BranchesListOptions.
func (o *BranchesListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.Name != "" {
v.Add("name", o.Name)
Expand Down
22 changes: 13 additions & 9 deletions crowdin/model/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,21 @@ type ProjectsListOptions struct {

// Values returns the url.Values representation of ProjectsListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (p *ProjectsListOptions) Values() (url.Values, bool) {
v, _ := p.ListOptions.Values()
if p.UserID > 0 {
v.Add("userId", fmt.Sprintf("%d", p.UserID))
func (o *ProjectsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}
if p.HasManagerAccess != nil &&
(*p.HasManagerAccess == 0 || *p.HasManagerAccess == 1) {
v.Add("hasManagerAccess", fmt.Sprintf("%d", *p.HasManagerAccess))

v, _ := o.ListOptions.Values()
if o.UserID > 0 {
v.Add("userId", fmt.Sprintf("%d", o.UserID))
}
if o.HasManagerAccess != nil &&
(*o.HasManagerAccess == 0 || *o.HasManagerAccess == 1) {
v.Add("hasManagerAccess", fmt.Sprintf("%d", *o.HasManagerAccess))
}
if p.Type != nil && (*p.Type == 0 || *p.Type == 1) {
v.Add("type", fmt.Sprintf("%d", *p.Type))
if o.Type != nil && (*o.Type == 0 || *o.Type == 1) {
v.Add("type", fmt.Sprintf("%d", *o.Type))
}
return v, len(v) > 0
}
Expand Down
12 changes: 12 additions & 0 deletions crowdin/model/source_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type DirectoryListOptions struct {

// Values returns the url.Values representation of DirectoryListOptions.
func (o *DirectoryListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.BranchID > 0 {
v.Add("branchId", fmt.Sprintf("%d", o.BranchID))
Expand Down Expand Up @@ -167,6 +171,10 @@ type FileListOptions struct {

// Values returns the url.Values representation of FileListOptions.
func (o *FileListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.BranchID > 0 {
v.Add("branchId", fmt.Sprintf("%d", o.BranchID))
Expand Down Expand Up @@ -537,6 +545,10 @@ type ReviewedBuildListOptions struct {

// Values returns the url.Values representation of ReviewedBuildListOptions.
func (o *ReviewedBuildListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.BranchID > 0 {
v.Add("branchId", fmt.Sprintf("%d", o.BranchID))
Expand Down
8 changes: 8 additions & 0 deletions crowdin/model/source_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type SourceStringsListOptions struct {
// Values returns the url.Values representation of SourceStringListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *SourceStringsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.DenormalizePlaceholders != nil &&
(*o.DenormalizePlaceholders == 0 || *o.DenormalizePlaceholders == 1) {
Expand Down Expand Up @@ -113,6 +117,10 @@ type SourceStringsGetOptions struct {

// Values returns the url.Values representation of SourceStringsGetOptions.
func (o *SourceStringsGetOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v := url.Values{}
if o.DenormalizePlaceholders != nil &&
(*o.DenormalizePlaceholders == 0 || *o.DenormalizePlaceholders == 1) {
Expand Down
20 changes: 20 additions & 0 deletions crowdin/model/string_translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type ApprovalsListOptions struct {
// Values returns the url.Values representation of the ApprovalsListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *ApprovalsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.FileID > 0 {
v.Add("fileId", fmt.Sprintf("%d", o.FileID))
Expand Down Expand Up @@ -204,6 +208,10 @@ type LanguageTranslationsListOptions struct {
// Values returns the url.Values representation of the LanguageTranslationsListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *LanguageTranslationsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if len(o.StringIDs) > 0 {
v.Add("stringIds", joinIntSlice(o.StringIDs))
Expand Down Expand Up @@ -266,6 +274,10 @@ type TranslationGetOptions struct {
// Values returns the url.Values representation of the TranslationGetOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *TranslationGetOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v := url.Values{}
if o.DenormalizePlaceholders != nil &&
(*o.DenormalizePlaceholders == 0 || *o.DenormalizePlaceholders == 1) {
Expand Down Expand Up @@ -293,6 +305,10 @@ type StringTranslationsListOptions struct {
// Values returns the url.Values representation of the StringTranslationsListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *StringTranslationsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.StringID > 0 {
v.Add("stringId", fmt.Sprintf("%d", o.StringID))
Expand Down Expand Up @@ -391,6 +407,10 @@ type VotesListOptions struct {
// Values returns the url.Values representation of the VotesListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *VotesListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.StringID > 0 {
v.Add("stringId", fmt.Sprintf("%d", o.StringID))
Expand Down
8 changes: 8 additions & 0 deletions crowdin/model/translation_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type ProjectProgressListOptions struct {
// Values returns the url.Values representation of ProjectProgressListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *ProjectProgressListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.LanguageIDs != "" {
v.Add("languageIds", o.LanguageIDs)
Expand Down Expand Up @@ -94,6 +98,10 @@ type QACheckListOptions struct {
// Values returns the url.Values representation of QACheckListOptions.
// It implements the crowdin.ListOptionsProvider interface.
func (o *QACheckListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.Category != "" {
v.Add("category", o.Category)
Expand Down
12 changes: 8 additions & 4 deletions crowdin/model/translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,14 @@ type TranslationsBuildsListOptions struct {
}

// Values returns the url.Values representation of the query options.
func (p *TranslationsBuildsListOptions) Values() (url.Values, bool) {
v, _ := p.ListOptions.Values()
if p.BranchID > 0 {
v.Add("branchId", fmt.Sprintf("%d", p.BranchID))
func (o *TranslationsBuildsListOptions) Values() (url.Values, bool) {
if o == nil {
return nil, false
}

v, _ := o.ListOptions.Values()
if o.BranchID > 0 {
v.Add("branchId", fmt.Sprintf("%d", o.BranchID))
}

return v, len(v) > 0
Expand Down

0 comments on commit 625bdff

Please sign in to comment.