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
-
-
-
-
-
-
package credly
-
-import (
- "bytes"
- "encoding/json"
- "fmt"
- "net/http"
- "time"
-)
-
-// https://www.credly.com/docs/issued_badges
-type issueBadgeResponse struct {
- Data BadgeInfo `json:"data"`
-}
-
-type getBadgesResponse struct {
- Data []BadgeInfo `json:"data"`
-}
-
-type getBadgeTemplateResponse struct {
- Data BadgeTemplate `json:"data"`
-}
-type getBadgeTemplatesResponse struct {
- Data []BadgeTemplate `json:"data"`
-}
-
-type BadgeInfo struct {
- Id string `json:"id"`
- ImageUrl string `json:"image_url"`
- Url string `json:"badge_url"`
- IssuedAt time.Time `json:"issued_at"`
- State string `json:"state"`
-
- Image struct {
- Url string `json:"url"`
- } `json:"image"`
-
- Template BadgeTemplate `json:"badge_template"`
-
- User struct {
- Id string `json:"id"`
- Email string `json:"email"`
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- Url string `json:"url"`
- } `json:"user"`
-}
-
-type BadgeTemplate struct {
- Id string `json:"id,omitempty"`
- Name string `json:"name"`
- Skills []string `json:"skills"`
- Url string `json:"url"`
- ImageUrl string `json:"image_url"`
- VanitySlug string `json:"vanity_slug"`
-}
-
-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)
-
- now := time.Now()
- issuedAt := now.Format("2006-01-02 15:04:05 -0700")
-
- params := map[string]interface{}{
- "badge_template_id": templateId,
- "recipient_email": email,
- "issued_to_first_name": firstName,
- "issued_to_last_name": lastName,
- "issued_at": issuedAt,
- }
- reqBody, err := json.Marshal(params)
- if err != nil {
- return i, fmt.Errorf("[credly.IssueBadge] Failed to marshal filter: %v", err)
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
- if err != nil {
- return i, err
- }
-
- resp, err := c.Do(req)
- if err != nil {
- return i, err
- }
-
- defer resp.Body.Close()
-
- if resp.StatusCode == http.StatusUnprocessableEntity {
- // Contact already has badge
- return i, fmt.Errorf(ErrBadgeAlreadyIssued)
- }
-
- if resp.StatusCode != http.StatusCreated {
- return i, fmt.Errorf("[credly.IssueBadge] API request failed with status code: %d", resp.StatusCode)
- }
-
- var badgeResp issueBadgeResponse
- if err := json.NewDecoder(resp.Body).Decode(&badgeResp); err != nil {
- return i, fmt.Errorf("[credly.IssueBadge] Failed to parse JSON data: %v", err)
- }
-
- return badgeResp.Data, nil
-}
-
-func (c *Client) GetBadges(email string) (b []BadgeInfo, err error) {
- url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", OrganizationId)
- url = fmt.Sprintf("%s?filter=recipient_email::%s", url, email)
-
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return b, err
- }
-
- resp, err := c.Do(req)
- if err != nil {
- return b, err
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return b, fmt.Errorf("[credly.GetBadges] API request failed with status code: %d", resp.StatusCode)
- }
-
- var badgesResp getBadgesResponse
- if err := json.NewDecoder(resp.Body).Decode(&badgesResp); err != nil {
- return b, fmt.Errorf("[credly.GetBadges] Failed to parse JSON data: %v", err)
- }
-
- return badgesResp.Data, nil
-}
-
-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("%s?filter=recipient_email::%s|badge_template_id::%s", url, email, badgeId)
-
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return b, err
- }
-
- resp, err := c.Do(req)
- if err != nil {
- return b, err
- }
- defer resp.Body.Close()
-
- var badgesResp getBadgesResponse
- if err := json.NewDecoder(resp.Body).Decode(&badgesResp); err != nil {
- return b, fmt.Errorf("Failed to parse JSON data: %v", err)
- }
-
- if len(badgesResp.Data) == 0 {
- return b, nil
- }
-
- return badgesResp.Data[0], nil
-}
-
-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)
-
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return b, err
- }
-
- resp, err := c.Do(req)
- if err != nil {
- return b, err
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return b, fmt.Errorf("[credly.GetBadgeTemplate] API request failed with status code: %d", resp.StatusCode)
- }
-
- var badgeResp getBadgeTemplateResponse
- if err := json.NewDecoder(resp.Body).Decode(&badgeResp); err != nil {
- return b, fmt.Errorf("[credly.GetBadgeTemplate] Failed to parse JSON data: %v", err)
- }
-
- return badgeResp.Data, nil
-}
-
-func (c *Client) GetBadgeTemplates() (b []BadgeTemplate, err error) {
- url := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badge_templates", OrganizationId)
-
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return b, err
- }
-
- resp, err := c.Do(req)
- if err != nil {
- return b, err
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return b, fmt.Errorf("[credly.GetBadgeTemplates] API request failed with status code: %d", resp.StatusCode)
- }
-
- var badgeResp getBadgeTemplatesResponse
- if err := json.NewDecoder(resp.Body).Decode(&badgeResp); err != nil {
- return b, fmt.Errorf("[credly.GetBadgeTemplates] Failed to parse JSON data: %v", err)
- }
-
- return badgeResp.Data, nil
-}
-
-
-
package credly
-
-import (
- "encoding/base64"
- "net/http"
-)
-
-// HTTPClientInterface defines the methods that http.Client and MockHTTPClient must implement
-type HTTPClientInterface interface {
- Do(req *http.Request) (*http.Response, error)
-}
-
-type Client struct {
- HTTPClient HTTPClientInterface
- authToken string
-}
-
-const (
- OrganizationId = "6f640a21-8552-4b27-b02f-25c3a2588e17"
- ErrBadgeAlreadyIssued = "User already has this badge"
-)
-
-func NewClient(token string) *Client {
- encodedToken := base64.StdEncoding.EncodeToString([]byte(token + "|"))
-
- return &Client{
- HTTPClient: &http.Client{},
- authToken: encodedToken,
- }
-}
-
-func (c *Client) Do(req *http.Request) (*http.Response, error) {
- req.Header.Set("Authorization", "Basic "+c.authToken)
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Accept", "application/json")
- return c.HTTPClient.Do(req)
-}
-
-
-
-
-
-