Skip to content

Commit

Permalink
Merge pull request UnlockedLabs#54 from PThorpe92/canvasServiceFixes
Browse files Browse the repository at this point in the history
fix: correct errors in canvas service
  • Loading branch information
nokierae authored Jan 16, 2024
2 parents 4247441 + 6e5c007 commit 755fdba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
57 changes: 54 additions & 3 deletions app/Services/CanvasServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use App\Models\ProviderPlatform;
use App\Models\User;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -27,6 +28,7 @@
const GRADEABLE_STUDENTS = 'gradeable_students/';
const READ = 'read/';
const ANONYMOUS_SUBMISSIONS = 'anonymous_submissions/';
const LOGINS = 'logins/';

class CanvasServices
{
Expand All @@ -42,8 +44,23 @@ class CanvasServices

public Client $client;

// Constructor, if the URL is missing the protocol or api version , add it.
public function __construct(int $providerId, int $accountId, string $apiKey, string $url)
{

$parsedUrl = parse_url($url);
if (! isset($parsedUrl['scheme'])) {
$url = 'https://'.$url;
}

if (! isset($parsedUrl['path']) || $parsedUrl['path'] !== CANVAS_API) {
$url .= CANVAS_API;
}

if ($accountId === 0 || $accountId === null) {
$accountId = 'self';
}

$this->provider_id = $providerId;
$this->account_id = $accountId;
$this->access_key = $apiKey;
Expand All @@ -59,7 +76,7 @@ public function getAccountId(): int

public function getAccessKey(): string
{
return $this->base_url;
return $this->access_key;
}

public function getBaseUrl(): string
Expand All @@ -72,7 +89,7 @@ public function getBaseUrl(): string
*/
public function getClient(): Client
{
return new Client(['Headers' => ['Authorization' => 'Bearer'.$this->access_key]]);
return new Client(['Headers' => ['Authorization' => 'Bearer'.$this->getAccessKey()]]);
}

// Returns an instance of CanvasServices to make dynamic API calls.
Expand All @@ -92,7 +109,7 @@ public static function byProviderId(int $providerId): CanvasServices|InvalidArgu
}

/**
* validate and format the account ID parameter for API URLs
* validate and format the account ID parameter for API URis
*
* @return string Formatted account or user ID
*
Expand All @@ -116,6 +133,39 @@ public static function handleResponse(ResponseInterface $response): mixed
}
}

/**
* Create a new login to a given provider for a given User.
*
*
* @return mixed JSON decoded
*/
public static function createUserLogin(int $userId, int $providerId, string $authProviderId = 'openid_connect')
{
$canvasService = self::byProviderId($providerId);
$user = User::findOrFail($userId);
$accountId = $canvasService['account_id'];
$canvasUrl = $canvasService->api_url;
$token = $canvasService['access_key'];
try {
$response = $canvasService->client->post($canvasUrl.ACCOUNTS.$accountId.'/logins', [
'form_params' => [
'user[id]' => $user->id,
'login[unique_id]' => $user->email,
'login[password]' => $user->password,
'login[authentication_provider_id]' => $authProviderId,
],
'headers' => [
'Authorization' => "Bearer $token",
],
]);

return response()->json(['message' => 'Login created successfully in Canvas', 'data' => json_decode((string) $response->getBody())], 200);
} catch (\Exception $e) {

return response()->json(['error' => 'Failed to create login in Canvas', 'message' => $e->getMessage()], 500);
}
}

/**
* Get a list of users from Canvas
*
Expand Down Expand Up @@ -182,6 +232,7 @@ public function createStudentInCanvas(string $name, string $email, bool $terms =
'force_validations' => true,
];
$base_url = $this->api_url.ACCOUNTS.self.USERS;

try {
$response = $this->client->post($base_url, $userData);
} catch (RequestException $e) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 755fdba

Please sign in to comment.