- 22 Dec 2023
- 9 Minutes to read
- Print
- DarkLight
Cogniac C# SDK
- Updated on 22 Dec 2023
- 9 Minutes to read
- Print
- DarkLight
.NET 4.6.1 C# SDK For Cogniac Public API
This client library provides access to most of the common functionality of the Cogniac public API. The main entry point is the Cogniac.Connection object.
The namespace Cogniac is used in this SDK, and all trivial types are nullable (E.G. long? ExpiresIn;).
Class: Connection
Description | Create an authenticated Cogniac connection with known credentials. |
username | (optional) |
password | (optional) |
tenantId | (optional) |
token | (optional) |
urlPrefix | (optional) |
autoRenewToken | (optional) |
The following methods are members of Cogniac.Connection object:
GetAllAuthorizedTenants(username, password, urlPrefix)
Description | Static method that returns an AuthorizedTenants object containing all tenants that a specific user |
Return | Cogniac.Tenants - multi-member object. |
GetAuth()
Description | Returns an object containing the authentication information with the Cogniac API. |
Return | Cogniac.Auth - multi-member object. |
UploadMedia(forceSet, fileName, mediaTimestamp, forceOverwrite, metaTags, isPublic, externalMediaId, originalUrl, originalLandingUrl, license, authorProfileUrl, author, title, sourceUrl, previewUrl, localGatewayUrl)
Description | Uploads a media file to the Cogniac system. |
forceSet | (optional) |
fileName | (optional) |
mediaTimestamp | (optional) |
forceOverwrite | (optional) |
metaTags | (optional) |
isPublic | (optional) |
externalMediaId | (optional) |
originalUrl | (optional) |
originalLandingUrl | (optional) |
license | (optional) |
authorProfileUrl | (optional) |
title | (optional) |
sourceUrl | (optional) |
previewUrl | (optional) |
localGatewayUrl | (optional) |
Return | Cogniac.Media - multi-member object. |
DeleteMedia(mediaId, localGatewayUrl)
Description | Deletes a specific media file from the Cogniac system. |
mediaId | (required) |
Return | Boolean - 'true' on success, 'false' otherwise. |
GetAllSubjects(tenantId)
Description | Gets all subjects associated with a given tenant ID. |
tenantId | (required) |
Return | Cogniac.Subjects - multi-member object. |
GetSubject(subjectUid)
Description | Gets a subject associated with a given subject UID. |
subjectUid | (required) |
Return | Cogniac.Subject - multi-member object. |
GetAllApplications(tenantId)
Description | Gets all applications associated with a given tenant ID. |
tenantId | (required) |
Return | Cogniac.Applications - multi-member object. |
GetApplication(applicationId)
Description | Gets an application associated with a given application ID. |
applicationId | (required) |
Return | Cogniac.Application - multi-member object. |
GetTenant(tenantId)
Description | Gets a tenant's information given a tenant ID. |
tenantId | (required) |
Return | Cogniac.Tenant - multi-member object. |
AssociateMediaToSubject(mediaId, subjectUid, forceFeedback)
Description | Associates an uploaded media to a given subject. |
mediaId | (required) |
subjectUid | (required) |
foreFeedback | (required) |
Return | Cogniac.Tenant - multi-member object. |
GetMedia(mediaId)
Description | Gets a Cogniac.Media object from a given media ID. |
mediaId | (required) |
Return | Cogniac.Media - multi-member object. |
CreateSubject(name, description, publicRead, publicWrite)
Description | Create a subject in the Cogniac system. |
name | (required) |
description | (optional) |
publicRead | (optional) |
publicWrite | (optional) |
Return | Cogniac.Subject - multi-member object. |
DeleteSubject(subjectUid)
Description | Deletes a subject from the Cogniac system. |
subjectUid | (required) |
Return | Boolean - 'true' on success, 'false' otherwise. |
CreateApplication(name, type, description, inputSubjects, outputSubjects, releaseMetrics, detectionThresholds, detectionPostUrls, gatewayPostUrls, active, requestedFeedbackPerHour, refreshFeedback, appManagers)
Description | Creates an application in the Cogniac system. |
name | (required) |
type | (required) |
description | (optional) |
inputSubjects | (optional) |
outputSubjects | (optional) |
releaseMetrics | (optional) |
detectionThresholds | (optional) |
detectionPostUrls | (optional) |
gatewayPostUrls | (optional) |
active | (optional) |
requestedFeedbackPerHour | (optional) |
refreshFeedback | (optional) |
appManagers | (optional) |
Return | Cogniac.Application - multi-member object. |
GetSubjectMediaAssociations(subjectUid)
Description | Gets the subject media association given a subject UID. |
subjectUid | (required) |
Return | Cogniac.SubjectMediaAssociations - multi-member object. |
GetMediaSubjects(mediaId)
Description | Gets the subjects associated to a given media ID. |
mediaId | (required) |
Return | Cogniac.MediaSubjects - multi-member object. |
For complete class definitions and detailed member descriptions, please visit: https://github.com/Cogniac/cogniac-sdk-csharp
SDK Usage Examples
All the examples assume: 'using Cogniac;'
Connecting to Cogniac with username, password, and tenant ID.
var cc = new Connection("someUser@company.com", "MyPassword", "ValidTenantID");
if (cc != null)
{
var ao = cc.GetAuthObject();
if (ao != null)
{
// Get the token for later use
string token = ao.AccessToken;
}
}
Connecting to Cogniac with a token.
string token = "ValidTokenSequence";
var cc = new Connection cc = new Connection(token: token);
if (cc != null)
{
// The rest of the program
}
Connecting to Cogniac with a username and password but no tenant ID. (username is assumed to have 1 tenant)
var at = Connection.GetAllAuthorizedTenants("someUser@company.com", "MyPassword");
if (at != null)
{
// Object 'at' will contain all the authorized tenants of this user, we use the first one
var cc = new Connection("someUser@company.com", "MyPassword", at.Tenants[0].TenantId);
var ao = cc.GetAuthObject();
if (ao != null)
{
// Get the token for later use
string token = ao.AccessToken;
}
}
The following examples will assume a Cogniac.Connection object 'cc' has already been created properly.
Uploading a media item and associating it with a subject
string subjectUid = "KnownSubjectUid";
bool forceFeedback = true;
string[] tags = new string[] {"Media Owner", "BlackBerry KeyOne", "Android 7.1.1"};
string fullFileName = "Path\To\Image.jpg";
var m = cc.UploadMedia(fileName: fullFileName, metaTags: tags, forceOverwrite: true, isPublic: false);
if (m != null)
{
var ci = _con.AssociateMediaToSubject(m.MediaId, subjectUid, forceFeedback);
if (ci != null)
{
Console.WriteLine($"Association successful. CaptureId: '{ci.Id}'");
}
}
Deleting a media item from the Cogniac system
string mediaId = "KnownMediaId";
if (cc.DeleteMedia(mediaId))
{
Console.WriteLine("Media deleted");
}
else
{
Console.WriteLine("Error deleting media");
}
Get subjects, subject, applications, application and tenant
string tenantId = "KnownTenantId";
string subjectUid = "KnownSubjectUid";
string appId = "KnownApplicationId";
var subjects = cc.GetAllSubjects(tenantId);
var subject cc.GetSubject(subjectUid);
var apps = cc.GetAllApplications(tenantId);
var app = cc.GetApplication(appId);
var t = cc.GetTenant(tenantId);
Create a Cogniac application
var app = cc.CreateApplication("TestApp", "classification");
if (app != null)
{
// Application created properly, view it in JSON
Console.WriteLine(Serialize.ToJson(app));
}
Create a Cogniac subject
var sub = cc.CreateSubject("test", "this is a test subject");
if (sub != null)
{
// Subject created properly, view it in JSON
Console.WriteLine(Serialize.ToJson(sub));
}
Get subject media associations
string subjectUid = "KnownSubjectUid";
var sma = cc.GetSubjectMediaAssociations(subjectUid);
Get media subjects
string mediaId = "KnownMediaId";
var ms = cc.GetMediaSubjects(mediaId);
Utility: CogUpload.exe
Depends on all the "Release" DLLs output from the 'CogniacCSharpSDK' project.
Usage: CogUpload [-OPTION1 [ARG1]] [-OPTION2 [ARG1] [ARG2] [ARG3] ...] [-OPTION3 [ARG1]] ...
* All options starts with '-' followed by a space after the option.
* Arguments to each option follow the option directly.
* If an option takes an array, the members are provided as space separated arguments.
* All options take a single string argument unless specified.
List of available options:
-fs | -ForceSet Either 'training' or 'validation', don't provide it otherwise.
-f | -FileName Full path and file name of meida.
-d | -Dirname Directory of media files to process.
-u | -Username Cogniac issued username.
-p | -Password Cogniac issued password.
-tid | -TenantId Valid Cogniac tenant ID.
-tk | -Token Valid Cogniac access token.
-up | -UrlPrefix URL prefix of the Cogniac API.
-lgu | -LocalGatewayUrl Local gateway URL.
-mt | -MediaTimestamp Time stamp of the media.
-ff | -ForceFeedback ['True' or 'False' (default)] Force feedback after upload.
-fow | -ForceOverwrite ['True' (default) or 'False'] Force overwrite of media.
-mtg | -MetaTags [Array] List of meta tags of the media.
-isp | -IsPublic ['True' or 'False' (default)] Set media to public.
-emid | -ExternalMediaId External media ID.
-ou | -OriginalUrl Original media URL.
-olu | -OriginalLandingUrl Original landing URL.
-l | -License License link or text of the media.
-apu | -AuthorProfileUrl Author profile URL.
-t | -Title Title of the media.
-su | -SourceUrl Source URL of the media.
-pu | -PreviewUrl Preview URL of the media.
-suid | -SubjectUid The Cogniac subject to associate the media with.
-r | -Recursive ['True' or 'False' (default)] Recursively upload files in 'DirName'.
-h | -Help Displays this help message.
Note 1: 'TenantId' must always be provided unless 'Token' is used.
Note 2: Either use 'Token' or 'Username' and 'Password' but not both. If both are provided 'Token'
will be used and 'Username' and 'Password' will be ignored.
Note 3: 'SubjectUid' must always be provided. It applies to all 'FileName' and/or 'DirName' uploads.
Note 4: 'if 'DirName' is provided, all media within the directory will be uploaded recursively.
Note 5: 'MediaTimestamp', 'ExternalMediaId', 'SourceUrl', 'OriginalUrl', 'OriginalLandingUrl',
'Title', 'SourceUrl' and 'PreviewUrl' are single media file options only and cannot be
applied to an entire directory. The rest of the options apply to every media file within
the provided directory to process.
Note 6: If both 'FileName and 'DirName' are provided, the single file will process first
then the entire directory will process second.
Note 7: Spaces in any string feild are NOT permitted, please use '+' instead.
For example: '-f my image.png' is invalid, use '-f my+image.png' instead.
Note 8: Options are NOT case-sensitive.
Example 1: CogUpload -f C:\Path\To\Image.png -u MYUSER -p MYPASSWORD -tid ABC123 -suid DEF456
Example 2: CogUpload -dirName C:\Path\To\Images -u MYUSER -p MYPASSWORD -tid ABC123 -suid DEF456
Example 3: CogUpload -fileName C:\Path\To\Image.png -token ABCDEF123456
-mtg John+Doe BlackBerry Android+7.1.1 -IsPublic True -ff True -suid DEF456