diff --git a/credly/badge.go b/credly/badge.go index 893de34..57cc006 100644 --- a/credly/badge.go +++ b/credly/badge.go @@ -58,7 +58,7 @@ type BadgeTemplate struct { } func (c *Client) IssueBadge(templateId string, email string, firstName string, lastName string) (i BadgeInfo, err error) { - url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", OrganizationId) + url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", c.OrganizationId) now := time.Now() issuedAt := now.Format("2006-01-02 15:04:05 -0700") @@ -105,7 +105,7 @@ func (c *Client) IssueBadge(templateId string, email string, firstName string, l } func (c *Client) GetBadges(email string, collections []string) (b []BadgeInfo, err error) { - qUrl := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", OrganizationId) + qUrl := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", c.OrganizationId) qUrl = fmt.Sprintf("%s?filter=recipient_email_all::%s", qUrl, url.QueryEscape(email)) if len(collections) > 0 { @@ -137,7 +137,7 @@ func (c *Client) GetBadges(email string, collections []string) (b []BadgeInfo, e } func (c *Client) GetBadge(email string, badgeId string) (b BadgeInfo, err error) { - url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", OrganizationId) + url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", c.OrganizationId) url = fmt.Sprintf("%s?filter=recipient_email_all::%s|badge_template_id::%s", url, email, badgeId) req, err := http.NewRequest("GET", url, nil) @@ -164,7 +164,7 @@ func (c *Client) GetBadge(email string, badgeId string) (b BadgeInfo, err error) } func (c *Client) GetBadgeTemplate(templateId string) (b BadgeTemplate, err error) { - url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badge_templates/%s", OrganizationId, templateId) + url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badge_templates/%s", c.OrganizationId, templateId) req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -190,7 +190,7 @@ func (c *Client) GetBadgeTemplate(templateId string) (b BadgeTemplate, err error } func (c *Client) GetBadgeTemplates() (b []BadgeTemplate, err error) { - url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badge_templates", OrganizationId) + url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badge_templates", c.OrganizationId) req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/credly/client.go b/credly/client.go index 997fc08..b647298 100644 --- a/credly/client.go +++ b/credly/client.go @@ -1,3 +1,5 @@ +// Package credly provides a client to interact with the Credly API, +// allowing operations such as issuing and retrieving badges for a specific organization. package credly import ( @@ -5,33 +7,57 @@ import ( "net/http" ) -// HTTPClientInterface defines the methods that http.Client and MockHTTPClient must implement +// HTTPClientInterface defines the methods that http.Client and MockHTTPClient must implement. +// This interface allows for mocking and testing of HTTP requests. type HTTPClientInterface interface { Do(req *http.Request) (*http.Response, error) } +// Client is a Credly API client used to interact with the organization's badges. +// It handles authentication and provides methods for making authorized HTTP requests. type Client struct { + // HTTPClient is the HTTP client used to make requests to the Credly API. HTTPClient HTTPClientInterface - authToken string + + // authToken is the base64-encoded authentication token for API access. + authToken string + + // OrganizationID is the unique identifier for the organization in Credly. + OrganizationId string } -const ( - OrganizationId = "6f640a21-8552-4b27-b02f-25c3a2588e17" - ErrBadgeAlreadyIssued = "User already has this badge" -) +// ErrBadgeAlreadyIssued indicates that a badge has already been issued to the user. +const ErrBadgeAlreadyIssued = "User already has this badge" -func NewClient(token string) *Client { +// NewClient creates a new instance of the Credly API client. +// It accepts an API token and the organization ID, returning a Client +// with an encoded authentication token and organization-specific settings. +// +// token: The API token provided by Credly for authentication. +// organizationId: The unique identifier for the organization in Credly. +// Returns: A new Client instance configured for Credly API interaction. +func NewClient(token, organizationId string) *Client { + // Encode the token with base64 and append a separator "|" encodedToken := base64.StdEncoding.EncodeToString([]byte(token + "|")) return &Client{ - HTTPClient: &http.Client{}, - authToken: encodedToken, + HTTPClient: &http.Client{}, + authToken: encodedToken, + OrganizationId: organizationId, } } +// Do sends an HTTP request using the Client's HTTP client, adding the necessary +// authentication headers for the Credly API. +// +// req: The HTTP request to be sent. +// Returns: The HTTP response and any error encountered. func (c *Client) Do(req *http.Request) (*http.Response, error) { + // Add the required headers for Credly API authentication and content type. req.Header.Set("Authorization", "Basic "+c.authToken) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") + + // Execute the HTTP request using the client's HTTP client. return c.HTTPClient.Do(req) } diff --git a/credly/client_test.go b/credly/client_test.go index 3d4cd30..9990379 100644 --- a/credly/client_test.go +++ b/credly/client_test.go @@ -21,9 +21,10 @@ func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { func TestNewClient(t *testing.T) { token := "test-token" + orgId := "abcd-efgh-1234-5678" expectedToken := base64.StdEncoding.EncodeToString([]byte(token + "|")) - client := NewClient(token) + client := NewClient(token, orgId) assert.NotNil(t, client.HTTPClient) assert.Equal(t, expectedToken, client.authToken) diff --git a/credly/coverage.html b/credly/coverage.html deleted file mode 100644 index 13989a0..0000000 --- a/credly/coverage.html +++ /dev/null @@ -1,353 +0,0 @@ - - - - - - credly: Go Coverage Report - - - -
- -
- not tracked - - not covered - covered - -
-
-
- - - - - -
- - -