GitHub
Setup
Insert the OneRoster folder into your project.
Usage
First, import the OneRoster package.
import ("../OneRoster")
Next, create a new instance of the OneRoster object, which takes in two strings,
the client_id and client_secret.
oneRoster := OneRoster.New("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
You are now able to make requests to a specified URL. This is done using the method MakeRosterRequest(url), which takes in the URL and returns the status code and response.
oneRoster := OneRoster.New("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
statusCode: response := oneRoster.MakeRosterRequest("https://example.com/users");
statusCode is an int containing the status code and response is a string with the JSON response.
fmt.Println(statusCode) // The status code
fmt.Println(response) // The JSON Response
Example
Example
Print all students' names
package main
// Import OneRoster
import (
"encoding/json"
"fmt"
"../OneRoster"
)
type User struct {
GivenName string
FamilyName string
}
type UserResponse struct {
Users []User
}
func getUsers(url string) []User {
// Create new OneRoster with the client_id and client_secret
oneRoster := OneRoster.New("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXX");
// Make the request to the given url and store the status code and response
statusCode, response := oneRoster.MakeRosterRequest(url)
b := []byte(response)
// If status_code is 200, create array of users from response, otherwise print message and return nil
if statusCode == 200 {
var userResponse = new(UserResponse)
json.Unmarshal(b, &userResponse)
return userResponse.Users
}
if statusCode == 401 {
fmt.Println("Unauthorized Request\n" + response)
} else if statusCode == 404 {
fmt.Println("Not found\n" + response)
} else if statusCode == 500 {
fmt.Println("Server Error\n" + response)
} else {
fmt.Println("Something Went Wrong, status code " + fmt.Sprint(statusCode) + "\n" + response)
}
return nil
}
// Print the users if not nil
func main() {
users := getUsers("https://example.com/users?fields=givenName%2CfamilyName&filter=role%3D'student'")
if users != nil {
for _, user := range users {
fmt.Println(user.GivenName + " " + user.FamilyName)
}
}
}