Skip to content

Commit

Permalink
feat: brightspace data model
Browse files Browse the repository at this point in the history
feat: add interface for brightspace provider service with necessary parameters
fix: add temp fix for go lint error so that this can be reviewed
feat: add initial brightspace data models used for deserialization and add utility methods
fix: add check for error per linting error, initial model work, expect changes and enhancements to occur
fix: correct file pathing issue and scope issue
  • Loading branch information
carddev81 authored Oct 31, 2024
1 parent f3c22c1 commit eede11e
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
30 changes: 29 additions & 1 deletion provider-middleware/brightspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func NewBrightspaceService(provider *models.ProviderPlatform, db *gorm.DB, param
//c save new refresh_token (tack it onto the end of the client secret separated by semicolon)
provider.AccessKey = brightspaceService.ClientSecret + ";" + brightspaceService.RefreshToken
if err := db.Debug().Save(&provider).Error; err != nil {
//send admin email??? maybe but not now
return nil, err
}
//d set headers that are required for requests to brightspace
Expand All @@ -67,6 +66,35 @@ func NewBrightspaceService(provider *models.ProviderPlatform, db *gorm.DB, param
return &brightspaceService, nil
}

func (srv *BrightspaceService) SendPostRequest(url string, data url.Values) (*http.Response, error) {
encodedUrl := data.Encode()
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(encodedUrl))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") //standard header for url.Values (encoded)
resp, err := srv.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}

func (srv *BrightspaceService) SendRequest(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
for key, value := range *srv.BaseHeaders {
req.Header.Add(key, value)
}
resp, err := srv.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}

func (srv *BrightspaceService) GetUsers(db *gorm.DB) ([]models.ImportUser, error) {
//get brightspace users
return nil, nil
Expand Down
133 changes: 133 additions & 0 deletions provider-middleware/brightspace_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"UnlockEdv2/src/models"
"archive/zip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)

type DataSetPlugin struct {
PluginId string `json:"PluginId"`
Name string `json:"Name"`
Description string `json:"Description"`
CreatedDate string `json:"CreatedDate"`
DownloadLink string `json:"DownloadLink"`
DownloadSize float64 `json:"DownloadSize"`
}

type BrightspaceUser struct {
UserId string `csv:"UserId"`
UserName string `csv:"UserName"`
OrgDefinedId string `csv:"OrgDefinedId"`
FirstName string `csv:"FirstName"`
LastName string `csv:"LastName"`
IsActive string `csv:"IsActive"`
Organization string `csv:"Organization"`
ExternalEmail string `csv:"ExternalEmail"`
}

type BrightspaceCourse struct {
OrgUnitId string `csv:"OrgUnitId"`
Organization string `csv:"Organization"`
Type string `csv:"Type"`
Name string `csv:"Name"`
Code string `csv:"Code"`
IsActive string `csv:"IsActive"`
IsDeleted string `csv:"IsDeleted"`
OrgUnitTypeId string `csv:"OrgUnitTypeId"`
}

type BrightspaceEnrollment struct {
OrgUnitId string `csv:"OrgUnitId"`
UserId string `csv:"UserId"`
RoleName string `csv:"RoleName"`
EnrollmentType string `csv:"EnrollmentType"`
}

func (kc *BrightspaceService) IntoImportUser(bsUser BrightspaceUser) *models.ImportUser {
return nil
}

func (kc *BrightspaceService) IntoCourse(bsCourse BrightspaceCourse) *models.Course {
return nil
}

func (srv *BrightspaceService) GetPluginId(pluginName string) (string, error) {
var pluginId string
resp, err := srv.SendRequest(DataSetsEndpoint)
if err != nil {
return pluginId, err
}
defer resp.Body.Close()
pluginData := []DataSetPlugin{}
if err = json.NewDecoder(resp.Body).Decode(&pluginData); err != nil {
return pluginId, err
}
for _, plugin := range pluginData {
if plugin.Name == pluginName {
pluginId = plugin.PluginId
break
} //end if
}
return pluginId, nil
}

func (srv *BrightspaceService) DownloadAndUnzipFile(targetDirectory string, targetFileName string, endpointUrl string) (string, error) {
//initial method for download/unzip file--WIP
var destPath string
resp, err := srv.SendRequest(endpointUrl)
if err != nil {
return destPath, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
zipFilePath := filepath.Join(targetDirectory, targetFileName)
file, err := os.Create(zipFilePath)
if err != nil {
return destPath, err
}
_, err = io.Copy(file, resp.Body)
if err != nil {
return destPath, err
}
file.Close()
zipFile, err := zip.OpenReader(zipFilePath) //open the zip file
if err != nil {
return destPath, err
}
defer zipFile.Close() //close it later
for _, zippedFile := range zipFile.File {
destPath = filepath.Join(targetDirectory, zippedFile.Name)
if zippedFile.FileInfo().IsDir() {
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
fmt.Println("error occurred while trying to make directories, error is: ", err)
}
continue
}
//there is going to be no directory for this file, as these are csv files
if err = os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
return destPath, err
}
outFile, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zippedFile.Mode())
if err != nil {
return destPath, err
}
defer outFile.Close()
rc, err := zippedFile.Open()
if err != nil {
return destPath, err
}
defer rc.Close()
_, err = io.Copy(outFile, rc)
if err != nil {
return destPath, err
}
}
}
return destPath, err
}

0 comments on commit eede11e

Please sign in to comment.