Skip to content

Commit

Permalink
Initial import
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 471d6ae commit 5a1c909
Show file tree
Hide file tree
Showing 8 changed files with 1,022 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Go

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23 # Specify a compatible Go version, like 1.20

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: go test ./... -v

216 changes: 216 additions & 0 deletions credly/badge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package credly

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"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, collections []string) (b []BadgeInfo, err error) {
qUrl := fmt.Sprintf("https://api.credly.com/v1/organizations/%s/badges", OrganizationId)
qUrl = fmt.Sprintf("%s?filter=recipient_email_all::%s", qUrl, url.QueryEscape(email))

if len(collections) > 0 {
colFilter := fmt.Sprintf("|badge_templates[reporting_tags]::%s", strings.Join(collections, ","))
qUrl = fmt.Sprintf("%s%s", qUrl, url.QueryEscape(colFilter))
}

req, err := http.NewRequest("GET", qUrl, 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_all::%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
}
Loading

0 comments on commit 5a1c909

Please sign in to comment.