User Tools

Site Tools


en:python:webapi

This is an old revision of the document!


Table of Contents

WebAPI

Let's try to make a quick win. We have to make a Proof Of Concept about something and writing a Web RestFul API is a good candidate.

Request

All Windows laptops in your company are supposed to execute a script every day. However you have a lot of tickets that may be explained by the fact that this daily script is not executed or too long and raises a kind of timeout.

Your idea is to log when the script starts and when it ends. A good one! However you have no idea how to quickly centralize these log files from all laptops. Why not a Web RestFul API? :-)

Before going further: yes I know that a lot of solutions may solve this issue but the target is to make the POC in less than one hour. As I used to develop Web Restful API then it is my choice.

Client side

From client side, we are going to use Powershell command as it is native to Windows 10.

client_side.ps1
# init
$who=whoami
$computer=hostname
$server="my-server.domain.com"
 
# before the main command, let's call the API
Invoke-WebRequest ("http://{2}:5000/my_action/start/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST
 
# main command
my_action
 
# after the main command, let's call the API
Invoke-WebRequest ("http://{2}:5000/my_action/stop/{0}/{1}" -f ($who -replace "\\", "@"), $computer, $server) -Method POST

API returns something like this:

StatusCode        : 201
StatusDescription : Created
Content           : {"user": "my_user", "action": "my_action", "ip_remote": "0.1.2.3", "computer":
                    "my_computer"}

RawContent        : HTTP/1.1 201 Created
                    Connection: close
                    Content-Length: 115
                    Content-Type: application/json
                    Date: Tue, 26 May 2020 13:46:28 GMT
                    Server: gunicorn/20.0.4

                    {"user": "my_user", "action": "my_acti...
Forms             : {}
Headers           : {[Connection, close], [Content-Length, 115], [Content-Type, application/json], [Date, Tue, 26 May 2020 13:46:28
                    GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 115
en/python/webapi.1618534769.txt.gz · Last modified: 2021/04/15 20:59 by lonclegr