Skip to content

Commit

Permalink
Pass Organization Id
Browse files Browse the repository at this point in the history
Signed-off-by: Raphaël Pinson <[email protected]>
  • Loading branch information
raphink committed Sep 11, 2024
1 parent 5a1c909 commit 765fd93
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 368 deletions.
10 changes: 5 additions & 5 deletions credly/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
44 changes: 35 additions & 9 deletions credly/client.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,63 @@
// 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 (
"encoding/base64"
"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)
}
3 changes: 2 additions & 1 deletion credly/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 765fd93

Please sign in to comment.