11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43using System . Text ;
54using LearnositySDK . Utils ;
65using System . Web ;
6+ using System . Runtime . InteropServices ;
77
88namespace LearnositySDK . Request
99{
@@ -77,6 +77,11 @@ public class Init
7777 /// </summary>
7878 private string algorithm ;
7979
80+ /// <summary>
81+ /// Determines if telemetry is enabled; default is true
82+ /// </summary>
83+ private static bool telemetryEnabled = true ;
84+
8085 /// <summary>
8186 /// Instantiate this class with all security and request data. It will be used to create a signature.
8287 /// </summary>
@@ -153,6 +158,7 @@ private void Initialize(string service, JsonObject securityPacket, string secret
153158 //try
154159 //{
155160 this . validate ( this . service , ref this . securityPacket , this . secret , this . requestPacket , this . action ) ;
161+ this . addTelemetryData ( ) ;
156162 this . requestString = this . generateRequestString ( ) ;
157163 this . setServiceOptions ( ) ;
158164 this . securityPacket . set ( "signature" , this . generateSignature ( ) ) ;
@@ -180,7 +186,7 @@ private string generateRequestString()
180186 {
181187 throw new Exception ( "Invalid data, please check you request packet." ) ;
182188 }
183-
189+
184190 return request ;
185191 }
186192
@@ -191,7 +197,7 @@ private string generateRequestString()
191197 /// - the `action` value if passed
192198 /// </summary>
193199 /// <returns>A signature hash for the request authentication</returns>
194- private string generateSignature ( )
200+ public string generateSignature ( )
195201 {
196202 List < string > signatureList = new List < string > ( ) ;
197203 string temp = null ;
@@ -256,7 +262,7 @@ public string generate()
256262 {
257263 return this . generateData ( output ) ;
258264 }
259- else if ( this . service == "assess" )
265+ else if ( this . service == "assess" )
260266 {
261267 output = this . generateAssess ( output ) ;
262268 }
@@ -277,7 +283,7 @@ public string generate()
277283 // do nothing
278284 break ;
279285 }
280-
286+
281287 return output . toJson ( ) ;
282288 }
283289
@@ -291,7 +297,7 @@ private string generateData(JsonObject output)
291297 StringBuilder sb = new StringBuilder ( ) ;
292298 JsonObject obj = null ;
293299 string str = "" ;
294-
300+
295301 obj = output . getJsonObject ( "security" ) ;
296302 if ( ! Tools . empty ( obj ) )
297303 {
@@ -380,7 +386,7 @@ private void setServiceOptions()
380386 case "assess" :
381387 // fall-through
382388 case "questions" :
383-
389+
384390 this . signRequestData = false ;
385391
386392 if ( this . service == "assess" && Tools . array_key_exists ( "questionsApiActivity" , this . requestPacket ) )
@@ -456,18 +462,21 @@ private void setServiceOptions()
456462 this . signRequestData = false ;
457463
458464 JsonObject requestPackageUsers = this . requestPacket . getJsonObject ( "users" ) ;
459- if ( requestPackageUsers != null ) {
460- string [ ] users = requestPackageUsers . getValuesArray ( ) ;
461- if ( users != null && users . Length > 0 ) {
465+ if ( requestPackageUsers != null )
466+ {
467+ string [ ] users = requestPackageUsers . getValuesArray ( ) ;
468+ if ( users != null && users . Length > 0 )
469+ {
462470 hashedUsers = new JsonObject ( ) ;
463- for ( int i = 0 ; i < users . Length ; i ++ ) {
471+ for ( int i = 0 ; i < users . Length ; i ++ )
472+ {
464473 string user_id = users [ i ] ;
465474 hashedUsers . set ( user_id , Tools . hash ( this . algorithm , user_id + this . secret ) ) ;
466475 }
467476 this . requestPacket . set ( "users" , hashedUsers ) ;
468477 }
469478 }
470-
479+
471480 break ;
472481 default :
473482 // do nothing
@@ -518,5 +527,111 @@ public void validate(string service, ref JsonObject securityPacket, string secre
518527 throw new Exception ( "The `secret` argument must be a valid string" ) ;
519528 }
520529 }
530+
531+ /// <summary>
532+ /// Adds a meta field with SDK information in it to the requestPacket
533+ /// </summary>
534+ public void addTelemetryData ( )
535+ {
536+ if ( this . isTelemetryEnabled ( ) )
537+ {
538+ JsonObject meta ;
539+ if ( this . requestPacket . getJsonObject ( "meta" ) != null )
540+ {
541+ meta = this . requestPacket . getJsonObject ( "meta" ) ;
542+ }
543+ else
544+ {
545+ meta = new JsonObject ( ) ;
546+ }
547+
548+ meta . set ( "sdk" , this . getSdkMeta ( ) ) ;
549+ this . requestPacket . set ( "meta" , meta ) ;
550+ }
551+ }
552+
553+ public bool isTelemetryEnabled ( )
554+ {
555+ return telemetryEnabled ;
556+ }
557+
558+ public JsonObject getSdkMeta ( )
559+ {
560+ JsonObject sdkMeta = new JsonObject ( ) ;
561+ sdkMeta . set ( "version" , this . getSdkVersion ( ) ) ;
562+ sdkMeta . set ( "lang" , "asp.net" ) ;
563+ sdkMeta . set ( "lang_version" , this . getLanguageVersion ( ) ) ;
564+ sdkMeta . set ( "platform" , this . getPlatform ( ) ) ;
565+ sdkMeta . set ( "platform_version" , this . getPlatformVersion ( ) ) ;
566+
567+ return sdkMeta ;
568+ }
569+
570+ /// <summary>
571+ /// Returns the version number of ASP.NET
572+ /// </summary>
573+ /// <returns></returns>
574+ public string getLanguageVersion ( )
575+ {
576+ return Environment . Version . ToString ( ) ;
577+ }
578+
579+ /// <summary>
580+ /// Returns the name of the platform the code is running on
581+ /// </summary>
582+ /// <returns></returns>
583+ public string getPlatform ( )
584+ {
585+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
586+ {
587+ return "darwin" ;
588+ }
589+
590+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
591+ {
592+ return "win" ;
593+ }
594+
595+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
596+ {
597+ return "linux" ;
598+ }
599+
600+ return Environment . OSVersion . Platform . ToString ( ) ;
601+ }
602+
603+ /// <summary>
604+ /// Returns the version number of the platform the code is running on
605+ /// </summary>
606+ /// <returns></returns>
607+ public string getPlatformVersion ( )
608+ {
609+ var os = Environment . OSVersion ;
610+ return string . Concat ( os . Version . Major , '.' , os . Version . Minor ) ;
611+ }
612+
613+ /// <summary>
614+ /// Extracts the version number of this SDK from the project meta data
615+ /// </summary>
616+ /// <returns>The version number</returns>
617+ public string getSdkVersion ( )
618+ {
619+ return GetType ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
620+ }
621+
622+ /// <summary>
623+ /// We use telemetry to enable better support and feature planning.
624+ /// It will not interfere with any usage.
625+ /// However, it is not advised to disable it.
626+ /// </summary>
627+ public static void disableTelemetry ( )
628+ {
629+ telemetryEnabled = false ;
630+ }
631+
632+ public static void enableTelemetry ( )
633+ {
634+ telemetryEnabled = true ;
635+ }
521636 }
522637}
0 commit comments