<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: New to API in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124875#M6067</link>
    <description>&lt;P&gt;Hi and tnx for reply&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;powershell is not an option since i am running the script from ubuntu server.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;tnx&lt;/P&gt;</description>
    <pubDate>Mon, 26 Jul 2021 06:08:07 GMT</pubDate>
    <dc:creator>alexkog</dc:creator>
    <dc:date>2021-07-26T06:08:07Z</dc:date>
    <item>
      <title>New to API</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124833#M6065</link>
      <description>&lt;P&gt;Hi guys&lt;/P&gt;&lt;P&gt;i am new to API world and i am trying ti automate some basic staff.&lt;/P&gt;&lt;P&gt;i am trying to work with the python script provided here &lt;A href="https://sc1.checkpoint.com/documents/latest/APIs/#introduction~v1.5%20" target="_blank"&gt;https://sc1.checkpoint.com/documents/latest/APIs/#introduction~v1.5%20&lt;/A&gt;&amp;nbsp;and just cant figure it out.&lt;/P&gt;&lt;P&gt;the firest error i got is :&lt;/P&gt;&lt;P&gt;Error(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))&lt;/P&gt;&lt;P&gt;i am manged to pass it by adding&amp;nbsp;verify=False to the&amp;nbsp;&lt;SPAN&gt;r&amp;nbsp;=&amp;nbsp;requests.post func.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;next error is:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;what am i doing wrong and what is the best way to debug it ?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;tnx&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 25 Jul 2021 12:24:53 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124833#M6065</guid>
      <dc:creator>alexkog</dc:creator>
      <dc:date>2021-07-25T12:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: New to API</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124852#M6066</link>
      <description>&lt;P&gt;I'm not terribly familiar with Python, so I'm not sure what could be wrong there. Is there any particular language you are comfortable with? There's probably somebody on the forum who could write a similar script in that language to help get you started. Here's basically the same script in PowerShell, which I know a bit better:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;$username = "my_username"
$password = "secret"
$managementServer = "192.168.65.2"
$chkpSID = ""

function Invoke-cpapiCall {
	param (
		[string]$apiPoint,
		[Hashtable]$callBody = @{}
	)
	
	$callResult = Invoke-WebRequest `
	-Uri "https://${managementServer}/web_api/${apiPoint}" `
	-ContentType "application/json"`
	-Method POST `
	-Headers @{"x-chkp-sid"=$chkpSID} `
	-Body $($callBody | ConvertTo-Json -Compress) `
	-SkipCertificateCheck `
	| ConvertFrom-Json -AsHashtable
	
	if ($apiPoint -eq "logout") {
		$chkpSID = ""
	}
	return $callResult
}

$loginBody = @{user=$username;password=$password}
$loginResult = Invoke-cpapiCall -apiPoint "login" -callBody $loginBody
$chkpSID = $loginResult["sid"]
Write-Host "Session ID: ${chkpSID}"

$newHostBody = @{name="new host name";"ip-address"="192.168.1.1"}
$newHostResult = Invoke-cpapiCall -apiPoint "add-host" -callBody $newHostBody
Write-Host "add-host result:`n$($newHostResult | ConvertTo-Json)`n"

$publishResult = Invoke-cpapiCall -apiPoint "publish"
Write-Host "publish result:`n$($publishResult | ConvertTo-Json)`n"

$logoutResult = Invoke-cpapiCall -apiPoint "logout"
Write-Host "logout result:`n$($logoutResult | ConvertTo-Json)"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This one keeps the request bodies as PowerShell hash tables by default. I like PowerShell because it comes with things like Invoke-WebRequest and Convert(To|From)-Json without needing to import any external libraries.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Jul 2021 23:32:18 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124852#M6066</guid>
      <dc:creator>Bob_Zimmerman</dc:creator>
      <dc:date>2021-07-25T23:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: New to API</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124875#M6067</link>
      <description>&lt;P&gt;Hi and tnx for reply&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;powershell is not an option since i am running the script from ubuntu server.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;tnx&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 06:08:07 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124875#M6067</guid>
      <dc:creator>alexkog</dc:creator>
      <dc:date>2021-07-26T06:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: New to API</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124902#M6069</link>
      <description>&lt;P&gt;I have not used the api outside mgmt_cli of the mgmt console. So I can not answer your original question.&lt;/P&gt;&lt;P&gt;If you want to give &amp;nbsp;powershell a Try, powershell Core is available for linux. There are *.deb packages available from microsoft.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 12:46:39 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124902#M6069</guid>
      <dc:creator>aheilmaier</dc:creator>
      <dc:date>2021-07-27T12:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: New to API</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124934#M6070</link>
      <description>&lt;P&gt;Yep! I'm actually running PowerShell on my Mac where I do all my other development work.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;That earlier code is kind of gross. Global variables and all that. I only spent ~20 minutes writing and testing it, though. Here's a version with somewhat better encapsulation:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;class CheckPointApiConnection {
	[String]$ManagementServer
	hidden [String]$CheckPointApiSid = ""
	
	CheckPointApiConnection(
		[String]$mgmtServer,
		[String]$User,
		[String]$Pass,
		[Boolean]$readOnly
	) {
		$this.ManagementServer = $mgmtServer
		$loginBody = @{user=$User;password=$Pass;"read-only"=$readOnly}
		$loginResult = $this.ApiCall("login", $loginBody, $false)
		$this.CheckPointApiSid = $loginResult["sid"]
	}
	
	[Hashtable] ApiCall(
		[String]$ApiPoint
	) {
		return $this.ApiCall($ApiPoint, @{}, $false)
	}
	
	[Hashtable] ApiCall(
		[String]$ApiPoint,
		[Hashtable]$Body
	) {
		return $this.ApiCall($ApiPoint, $Body, $false)
	}
	
	[Hashtable] ApiCall(
		[String]$ApiPoint,
		[Boolean]$PrintResult
	) {
		return $this.ApiCall($ApiPoint, @{}, $PrintResult)
	}
	
	[Hashtable] ApiCall(
		[String]$ApiPoint,
		[Hashtable]$Body,
		[Boolean]$PrintResult
	) {
		$CallResult = Invoke-WebRequest `
		-Uri "https://$($this.ManagementServer)/web_api/${ApiPoint}" `
		-ContentType "application/json"`
		-Method POST `
		-Headers @{"x-chkp-sid"=$($this.CheckPointApiSid)} `
		-Body $($Body | ConvertTo-Json -Compress) `
		-SkipCertificateCheck
		
		if ($apiPoint -eq "logout") {
			$this.CheckPointApiSid = ""
		}
		if ($PrintResult) {
			Write-Host "${ApiPoint} result:`n${CallResult}`n"
		}
		return ($CallResult | ConvertFrom-Json -AsHashtable)
	}
}

$apiConnection = [CheckPointApiConnection]::new("192.168.65.2", "my_username", "secret", $false)

$newHostBody = @{name="new host name";"ip-address"="192.168.1.1"}
$newHostResult = $apiConnection.ApiCall("add-host", $newHostBody, $true)

$publishResult = $apiConnection.ApiCall("publish", $true)

$logoutResult = $apiConnection.ApiCall("logout", $true)&lt;/LI-CODE&gt;
&lt;P&gt;I'm not as familiar with methods in PowerShell objects as I am with functions. It feels like all those signatures for ApiCall could probably be consolidated down with optional arguments, or default values. I'm not sure if I can add argument names to method calls. That would cost a little verbosity, but greatly improve clarity at the call site.&lt;/P&gt;
&lt;P&gt;This also doesn't have any error handling or disposal handling. For example, if you remove the object for the API connection, it should call logout to cleanly close the session on the management side.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 16:41:02 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-API/m-p/124934#M6070</guid>
      <dc:creator>Bob_Zimmerman</dc:creator>
      <dc:date>2021-07-26T16:41:02Z</dc:date>
    </item>
  </channel>
</rss>

