This article will guide you through creating an access level in BioStar 2 server via BioStar 2 API.
You can find a more detailed introduction to BioStar 2 New Local API in this article : [BioStar 2 API] How To Use BioStar 2 New Local API
In this article, you can learn how to call an API function to create an access level in your BioStar 2 server.
This article also includes a sample code of a C# program that incorporates the API, and also an example of API call made via Postman, a program used for building & testing RESTful API calls.
Part 1. API Call & Parameters
[POST] /api/access_levels
[Parameters]
Parameter | Type | Required | Description |
---|---|---|---|
name | String | Y | The name of the access level. Must be unique. |
description | String | N | Access level description |
access_level_items | Array | Y | Array of access level items. |
:door | Array | Y | Array of doors for access level item. |
:id | Number | Y | Id of door. |
:name | String | N | Name of door. |
:schedule_id | String | Y | Schedule for access level item. |
:id | Number | Y | id of schedule. |
:name | String | N | name of schedule. |
Part 2. Request Body & Response Model
[Example Value/Parameters Model]
* Please note that access level creation requires an existing schedule and door.
{ "AccessLevel": { "name": "as", "description": "string", "access_level_items": [ { "schedule_id": { "id": "1", "name": "Always" }, "doors": [ { "id": 8, "name": "door33" } ] } ] } } |
[Response Model]
{ "AccessLevel": { "id": "3", "name": "lvl_2" }, "DeviceResponse": { "rows": [ { "id": "541530960", "code": "64230" } ], "result": "false" }, "Response": { "code": "1003", "link": "https://support.supremainc.com/en/support/home", "message": "Success" }, "httpResponseStatus": 200 } |
[Response: Fail Example]
{ "Response": { "code": "10", "link": "https://support.supremainc.com/en/support/home", "message": "Login required." } } |
[Response: Successful Example]
{ "AccessLevel": { "id": "21", "name": "AccessLevelAPI" }, "DeviceResponse": { "rows": [ { "id": "541531089", "code": "0" } ], "result": "true" }, "Response": { "code": "0", "link": "https://support.supremainc.com/en/support/home", "message": "Success" } } |
Part 3. Console Create Access Levels Example
[Create Access Levels C# Method Source Code Example]
static async void CreateAccessLevels() { Console.WriteLine("*****CreateAccessLevels Task Started*****"); CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer;
HttpClient httpClient = new HttpClient(handler);
HttpClient client = new HttpClient(handler); httpClient.DefaultRequestHeaders.Add("bs-session-id", sessionID); cookieContainer.Add(new Uri(BioStarIP), new Cookie("bs-session-id", sessionID)); string resourceAddress = BioStarIP + "/api/access_levels";
JavaScriptSerializer serializer = new JavaScriptSerializer(); Console.WriteLine("Enter Access Level NAME: "); string name = Console.ReadLine(); Console.WriteLine("Enter Access Level DESCRIPTION: "); string description = Console.ReadLine(); SearchSchedules(); Console.WriteLine("Choose a SCHEDULE ID: "); string schedule = Console.ReadLine(); ListDoors(); Console.WriteLine("Choose a DOOR ID: "); string door = Console.ReadLine(); string payload = "{\"AccessLevel\": {\"name\": \"" + name + "\",\"description\": \"" + description + "\",\"access_level_items\": [{\"schedule_id\": { \"id\": \"" + schedule + "\"}, \"doors\": [{\"id\": " + door + "}]}]}}"; Console.WriteLine(payload); StringContent sc = new StringContent(payload, Encoding.UTF8, "application/json"); HttpResponseMessage httpResponse = httpClient.PostAsync(resourceAddress, sc).Result;
if (httpResponse.IsSuccessStatusCode == true) { Console.WriteLine("Create Access Level Successful."); string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine(httpResponseBody); } else { Console.WriteLine("Create Access Level Failed."); Console.WriteLine(httpResponse.ToString()); } } |
[After running the above code]
Part 4. Create Access Levels via Postman
[Request Example: Headers]
* Please note that you must obtain the ‘be-session-id’ value from the response header of the Login API call and input it in the request header of other APIs in order to authenticate other API calls.
[Request Example]
[Response Example: body]