@@ -381,90 +381,7 @@ namespace OIAPIExample
381
381
382
382
```
383
383
384
- ### Python 2 sample
385
- ``` python
386
- import json
387
- import requests
388
- import datetime
389
- import hashlib
390
- import hmac
391
- import base64
392
-
393
- # Update the customer ID to your Log Analytics workspace ID
394
- customer_id = ' xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
395
-
396
- # For the shared key, use either the primary or the secondary Connected Sources client authentication key
397
- shared_key = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
398
-
399
- # The log type is the name of the event that is being submitted
400
- log_type = ' WebMonitorTest'
401
-
402
- # An example JSON web monitor object
403
- json_data = [{
404
- " slot_ID" : 12345 ,
405
- " ID" : " 5cdad72f-c848-4df0-8aaa-ffe033e75d57" ,
406
- " availability_Value" : 100 ,
407
- " performance_Value" : 6.954 ,
408
- " measurement_Name" : " last_one_hour" ,
409
- " duration" : 3600 ,
410
- " warning_Threshold" : 0 ,
411
- " critical_Threshold" : 0 ,
412
- " IsActive" : " true"
413
- },
414
- {
415
- " slot_ID" : 67890 ,
416
- " ID" : " b6bee458-fb65-492e-996d-61c4d7fbb942" ,
417
- " availability_Value" : 100 ,
418
- " performance_Value" : 3.379 ,
419
- " measurement_Name" : " last_one_hour" ,
420
- " duration" : 3600 ,
421
- " warning_Threshold" : 0 ,
422
- " critical_Threshold" : 0 ,
423
- " IsActive" : " false"
424
- }]
425
- body = json.dumps(json_data)
426
-
427
- # ####################
428
- # #####Functions######
429
- # ####################
430
-
431
- # Build the API signature
432
- def build_signature (customer_id , shared_key , date , content_length , method , content_type , resource ):
433
- x_headers = ' x-ms-date:' + date
434
- string_to_hash = method + " \n " + str (content_length) + " \n " + content_type + " \n " + x_headers + " \n " + resource
435
- bytes_to_hash = bytes (string_to_hash).encode(' utf-8' )
436
- decoded_key = base64.b64decode(shared_key)
437
- encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod = hashlib.sha256).digest())
438
- authorization = " SharedKey {} :{} " .format(customer_id,encoded_hash)
439
- return authorization
440
-
441
- # Build and send a request to the POST API
442
- def post_data (customer_id , shared_key , body , log_type ):
443
- method = ' POST'
444
- content_type = ' application/json'
445
- resource = ' /api/logs'
446
- rfc1123date = datetime.datetime.utcnow().strftime(' %a , %d %b %Y %H:%M:%S GMT' )
447
- content_length = len (body)
448
- signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource)
449
- uri = ' https://' + customer_id + ' .ods.opinsights.azure.com' + resource + ' ?api-version=2016-04-01'
450
-
451
- headers = {
452
- ' content-type' : content_type,
453
- ' Authorization' : signature,
454
- ' Log-Type' : log_type,
455
- ' x-ms-date' : rfc1123date
456
- }
457
-
458
- response = requests.post(uri,data = body, headers = headers)
459
- if (response.status_code >= 200 and response.status_code <= 299 ):
460
- print ' Accepted'
461
- else :
462
- print " Response code: {} " .format(response.status_code)
463
-
464
- post_data(customer_id, shared_key, body, log_type)
465
- ```
466
-
467
- ### Python 3 sample
384
+ ### Python sample
468
385
``` python
469
386
import json
470
387
import requests
0 commit comments