in CODING, Python

Uploading Patch Definitions to Patch Server

I wrote a quick Python script to upload patch definitions hosted the external patch server by Bryson Tyrell for use with Jamf Pro’s patch management.

It’s a simple process of reading a directory, opening each file, loading the JSON then carrying out a requests to POST the data through the API

#!/usr/local/bin/python3

import requests
import json
from pathlib import Path

dirpath = 'path/to/patchdefinitions.json'
url="patchserver.url"
headers={"Content-Type": "application/json",}


def send_request(file_contents):
    try:
        response = requests.post(url = url,
            headers = headers,
            data = json.dumps(file_contents))
        print('Response HTTP Status Code: {status_code}'.format(
            status_code=response.status_code))
        print('Response HTTP Response Body: {content}'.format(
            content=response.content))
    except requests.exceptions.RequestException:
        print('HTTP Request failed')


def read_file(dirpath):
    pathlist = Path(dirpath).glob('*.json')
    for path in pathlist:
        # because path is object not string
        path_in_str = str(path)

        print(path_in_str, "\n")
        
        with open(path_in_str, 'r') as jf:
            file_contents = json.load(jf)

            send_request(file_contents)
            jf.close()

read_file(dirpath)