public async Task <CommandCentreConfiguration> InitialiseCommandCentreConfiguration(GglApiClient gglApi, IConfiguration configuration)
        {
            var keyPdfName         = configuration["pdfName"] ?? throw new Exception("pdfName not defined in configuration file");
            var keyAccessGroupName = configuration["accessGroupName"] ?? throw new Exception("accessGroupName not defined in configuration file");
            var keyCardTypeName    = configuration["cardTypeName"] ?? throw new Exception("cardTypeName not defined in configuration file");

            string keyPersonalDataFieldId;
            string studentAccessGroupHref;
            string rootDivisionHref;
            string cardholderSearchHref;
            string mobileCredentialTypeHref;

            Console.WriteLine("Startup: Connecting to the Command Centre REST api");

            // Always start with the entrypoint to the Command Centre REST api, which is /api
            var apiResponse = await gglApi.GetAsync <ApiResponse>("/api");

            // get the base url for the cardholders feature
            cardholderSearchHref = apiResponse.Features?.Cardholders?.Cardholders?.Href ??
                                   throw new Exception("ERROR: No cardholders feature in the REST api; Are you licensed for REST cardholders?");
            Console.WriteLine("Success: Found cardholderSearchHref; {0}", cardholderSearchHref);

            // look up the link for the root division to create new cardholders into
            var items = apiResponse.Features.Items ?? throw new Exception("ERROR: No items feature in the REST api; Are you licensed?");

            var itemsSearchHref  = items.Items?.Href;
            var divisionResponse = await gglApi.GetAsync <SearchResults <Item> >($"{itemsSearchHref}?type=15&name=\"Root Division\"");

            rootDivisionHref = divisionResponse.Results.FirstOrDefault()?.Href
                               ?? throw new Exception("ERROR: Can't find the root division");

            Console.WriteLine("Success: Found the Root Division; it's link is {0}", rootDivisionHref);

            // go look up the search key for the student ID pdf - type 33 is 'personal data field'
            // we expect a JSON response like this:
            // {
            //     "results": [
            //         {
            //             "id": "500",
            //             "name": "Student ID",
            //             "type": {
            //                 "id": "33",
            //                 "name": "Personal Data Field"
            //             }
            //         }
            //     ]
            // }
            var configPdfResponse = await gglApi.GetAsync <SearchResults <Item> >($"{itemsSearchHref}?type=33&name=\"{Uri.EscapeUriString(keyPdfName)}\""); // quoted name for exact string match

            keyPersonalDataFieldId = configPdfResponse.Results.FirstOrDefault()?.Id
                                     ?? throw new Exception($"ERROR: Can't find the personal data field with name of {keyPdfName}");

            Console.WriteLine("Success: Found PDF called {0}; it's lookup key ID is {1}", keyPdfName, keyPersonalDataFieldId);

            // find the Students access group (type == 2) which contains the Student ID pdf
            var accessGroupResponse = await gglApi.GetAsync <SearchResults <Item> >($"{itemsSearchHref}?type=2&name=\"{Uri.EscapeUriString(keyAccessGroupName)}\""); // quoted name for exact string match

            studentAccessGroupHref = accessGroupResponse.Results.FirstOrDefault()?.Href
                                     ?? throw new Exception($"ERROR: Can't find the access group with name of {keyAccessGroupName}");

            Console.WriteLine("Success: Found Access Group called {0}; it's link is {1}", keyAccessGroupName, studentAccessGroupHref);

            // go look up the search key for the mobile credential card type
            var cardTypesSearchHref = apiResponse.Features?.CardTypes?.CardTypes?.Href ?? throw new Exception("ERROR: No card types feature in the REST api; Are you licensed?");

            var cardTypesResponse = await gglApi.GetAsync <SearchResults <CardType> >($"{cardTypesSearchHref}?name=\"{Uri.EscapeUriString(keyCardTypeName)}\"");

            // we expect a JSON response like this:
            // {
            //     "results": [
            //         {
            //             "href": "https://localhost:8904/api/card_types/471",
            //             "id": "471",
            //             "name": "Mobile Credential",
            //             "credentialClass": "mobile"
            //         },
            //     ]
            // }
            mobileCredentialTypeHref = cardTypesResponse.Results.FirstOrDefault().Href
                                       ?? throw new Exception($"ERROR: Can't find the card type with name of {keyCardTypeName}");

            Console.WriteLine("Success: Found Card Type called {0}; it's link is {1}", keyAccessGroupName, mobileCredentialTypeHref);

            return(new CommandCentreConfiguration(
                       keyPersonalDataFieldId,
                       keyPdfName,
                       studentAccessGroupHref,
                       rootDivisionHref, cardholderSearchHref,
                       mobileCredentialTypeHref));
        }
 public StudentsController(Database db, GglApiClient gglApi, CommandCentreConfiguration commandCentreConfiguration)
 {
     _db     = db;
     _gglApi = gglApi;
     _commandCentreConfiguration = commandCentreConfiguration;
 }
Exemplo n.º 3
0
 public static Task <TResponse> PatchAsync <TResponse>(this GglApiClient apiClient, string url, Dictionary <string, object> body)
 => apiClient.RequestAsync <Dictionary <string, object>, TResponse>(HttpMethod.Patch, url, body);