import requests
import json

# 1. Create a session object
session = requests.Session()

# 2. Set the User-Agent for the session
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
session.headers.update({'User-Agent': user_agent})

# 3. Define and add cookies to the session
cookies_to_add = [
    ("SID", "g.a000vwi83sZx47d1q9Po8BgPR3jBX7lpsrzWSuPIKyp6RycUEctVzyy0oo1gNvQbwZa53AWllwACgYKAVQSARMSFQHGX2MiUpZ3JZlRTzUS5L-5fPmgpBoVAUF8yKprZzPlNM_X83070Ct35bq_0076", "/", ".google.com"),
    ("__Secure-1PSID", "g.a000vwi83sZx47d1q9Po8BgPR3jBX7lpsrzWSuPIKyp6RycUEctVKNqZm8qWU9ZC2DlRVPn0igACgYKAfQSARMSFQHGX2MiYGW66m_L_p1vlqiuEg-IrxoVAUF8yKrgRbA9tcA4VinZix0qlexX0076", "/", ".google.com"),
    ("__Secure-3PSID", "g.a000vwi83sZx47d1q9Po8BgPR3jBX7lpsrzWSuPIKyp6RycUEctVnGF-cJAl9Wr1rJ-NOCW63wACgYKAZASARMSFQHGX2Mi1gYcRRnI8v2AdvofwvFG5BoVAUF8yKooSw-opCZ1-vxmkQXCB7330076", "/", ".google.com"),
    ("HSID", "AJifEcy5MKbQSoqIz", "/", ".google.com"),
    ("SSID", "AB87p4RIXK_USto4D", "/", ".google.com"),
    ("APISID", "fQw9oTJbNnptFKdr/AIXxJuvoAk3qrIeWi", "/", ".google.com"),
    ("SAPISID", "3zFHAXrGoL_XNe0X/A7AafTXBL2NRj7N2h", "/", ".google.com"),
    ("__Secure-1PAPISID", "3zFHAXrGoL_XNe0X/A7AafTXBL2NRj7N2h", "/", ".google.com"),
    ("__Secure-3PAPISID", "3zFHAXrGoL_XNe0X/A7AafTXBL2NRj7N2h", "/", ".google.com"),
    ("AEC", "AVcja2e2MbzHDDL2yqJDMgUeDL-S3BEFB_yK293aRhQwshqv22Bd7IQ9mvg", "/", ".google.com"),
    ("NID", "523=SVwTZRVo9GJ5sSI0guSXNWwegQ7YY0h597wyCkrqkuIRMnQqpH3qrPA0SGB77KF69ibosOMUd-VNbNg1R7TXjPYFxYTUTP0uLtg41CaQctv6j1NKBaVPJoUkK3wzQ1e3l5yGsfBwGtypz5vxHP6yeyRtVWmYSiDiHr7AN1dKYXwzVVi7Dp8bvtppEAwUX7dQCqQ-4qGlHcoMW44STYwNELgd6CVXWPIhypl4qx_Wwcv7sY3GGP04s2kb0ljX7rYXKagj_Uv3xJDujDa4XUTAJKficyiWOrZT5LpeewUp1Kt5RyEBH2U7IHocmsWC6lfAKsoIt21wc1QwYioj9XkIIasJ4tCu1F7yXHjrglyGwbe5i67stMHI2FL3KXdoxe3ly_MORZWlVNM1Co7W43Rab01HMj2Ad940eXU9aV1PVzwlTtqiF0R0gLO_ubD6kykDRVMVMdMjMapz_dgIUfbkGMluSVykNRKnO7yf_rS7DKjXZXsh18UEsLcz6WjKtEYgmvm51MdH2JN5dcElSK_SWxrejJl-UW_eT_EqUQaeCtK2N7dTMFT38PGAkZFEKCvMqpjU86iLbFbmq_lKT-dm6IjIxFXj8yv652EYoCBlS3MsTjaQWP4_yRJa3oWhnB1kgwiBgBVmidRShZOT09L77WL6jVKE6YE09QrjGnoH_RjZFTwwGiuSFCLKfz9SwVsJBNV-nn5nZrU40kWmlhnfBNY3Kphq0fwO9RQ6wvzMAGzz684-kxzePSX3CgA74-rppEZrkXFMFjSaZGE-D7NAQp1qD2ez0-H2n0OXbRaLDN7JXn9fqhZu_FwvHJGfTBPZOZnk7taSnZSipmFU8gsTRQSVdFX6SlCXGpKueSRjhho", "/", ".google.com"),
    ("S", "maestro=IUwVdLn0rPZ27SY26uYPEonS9u1J9oQi8YzVJpqIc-w", "/", ".google.com"),
    ("__Secure-1PSIDTS", "sidts-CjIB7pHptaI1HakO1xLmYgvHilIKZJuufs1na9HjqCHJL13_z6LJNW13liGWofxE3NQ-NxAA", "/", ".google.com"),
    ("__Secure-3PSIDTS", "sidts-CjIB7pHptaI1HakO1xLmYgvHilIKZJuufs1na9HjqCHJL13_z6LJNW13liGWofxE3NQ-NxAA", "/", ".google.com"),
    ("_gid", "GA1.3.1639311329.1744393279", "/", ".lookerstudio.google.com"), # Different domain
    ("_ga", "GA1.1.1275703331.1743917675", "/", ".lookerstudio.google.com"), # Different domain
    ("_ga_LPCKLD3Z7X", "GS1.1.1744393286.6.0.1744393286.0.0.0", "/", ".lookerstudio.google.com"), # Different domain
    ("RAP_XSRF_TOKEN", "AImk1AJGP3sIKZ7N5-RA2jKHcJ0jdpxGKw:1744393288187", "/", "lookerstudio.google.com"), # Different domain (no leading dot)
    ("_gat", "1", "/", ".lookerstudio.google.com"), # Different domain
    ("SIDCC", "AKEyXzW9xrpFKS4Ox9-THfQ3DfB62JRx-bxxg0ZEiKYze2jaerhvWrVMFVCyjTjxIJfOhCOKQw4", "/", ".google.com"),
    ("__Secure-1PSIDCC", "AKEyXzXdMIev0GvM5xA0kMifj4jnuGZYNiob-2fJssX_jTBlwE8M4Bm9edS4J_i7UTSMFFEbCIm_", "/", ".google.com"),
    ("__Secure-3PSIDCC", "AKEyXzU8YysnVlR_9UcCx2GFo5hIUNPh6OqSCRE6Fpo9y12BNmobniOBCjTZc1_qHTS6VnivWX25", "/", ".google.com"),
    ("_ga_S4FJY0X3VX", "GS1.1.1744393279.7.1.1744393369.0.0.0", "/", ".lookerstudio.google.com") # Different domain
]

for name, value, path, domain in cookies_to_add:
    session.cookies.set(name=name, value=value, path=path, domain=domain)

# 4. Define the Target URL
url = "https://lookerstudio.google.com/batchedDataV2?appVersion=20250324_0406"

# 5. Define Headers (User-Agent and Cookies are handled by the session)
headers = {
    "authority": "lookerstudio.google.com",
    "accept": "application/json, text/plain, */*",
    "accept-encoding": "gzip, deflate, br, zstd", # requests usually handles this, but specifying can be safer
    "accept-language": "en-US,en;q=0.9",
    "cache-control": "no-cache",
    # "encoding": "null", # This header seems unusual/invalid for requests, omitting
    "origin": "https://lookerstudio.google.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://lookerstudio.google.com/reporting/1413fcfb-1416-4e56-8967-55f8e9f30ec8/page/p_pbm4eo88qc",
    "sec-ch-ua": '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-client-data": "CIS2yQEIorbJAQipncoBCMHbygEIk6HLAQiFoM0BCP6lzgEIvdXOAQjJ4M4BCIbizgEIu+fOAQjS6M4BCKzpzgE=",
    "x-rap-xsrf-token": "AImk1AJGP3sIKZ7N5-RA2jKHcJ0jdpxGKw:1744393288187",
    # Content-Type will be set automatically by requests when using json=payload
}

# 6. Define the JSON Body (Payload)
# Use triple quotes for the multi-line string and parse it with json.loads
body_string = """
{
  "dataRequest": [
    {
      "requestContext": {
        "reportContext": {
          "reportId": "1413fcfb-1416-4e56-8967-55f8e9f30ec8",
          "pageId": "p_pbm4eo88qc",
          "mode": 1,
          "componentId": "cd-rdkny9a9qc",
          "displayType": "simple-table",
          "actionId": "crossFilters"
        },
        "requestMode": 0
      },
      "datasetSpec": {
        "dataset": [
          {
            "datasourceId": "c2fc8cdd-46bb-454c-bf09-90ebfd4067d7",
            "revisionNumber": 0,
            "parameterOverrides": []
          }
        ],
        "queryFields": [
          {
            "name": "qt_3nwfu9yq1c",
            "datasetNs": "d0",
            "tableNs": "t0",
            "resultTransformation": {
              "analyticalFunction": 0,
              "isRelativeToBase": false
            },
            "dataTransformation": {
              "sourceFieldName": "_Event_Id_"
            }
          },
          {
            "name": "qt_8yjok4izsc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "resultTransformation": {
              "analyticalFunction": 0,
              "isRelativeToBase": false
            },
            "dataTransformation": {
              "sourceFieldName": "_DateTime_EST_"
            }
          },
          {
            "name": "qt_sfkc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_KYTC_Type_"
            }
          },
          {
            "name": "qt_4e66idhbrc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_Incident_Source_"
            }
          },
          {
            "name": "qt_re76qrqe2c",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_District_",
              "aggregation": 0
            }
          },
          {
            "name": "qt_tfkc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_County_Name_"
            }
          },
          {
            "name": "qt_ufkc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_Route_Label_"
            }
          },
          {
            "name": "qt_vfkc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_BMP_Initial_"
            }
          },
          {
            "name": "qt_o7kc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_EMP_Initial_"
            }
          },
          {
            "name": "qt_p7kc163arc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_Description_"
            }
          }
        ],
        "sortData": [
          {
            "sortColumn": {
              "name": "qt_8yjok4izsc",
              "datasetNs": "d0",
              "tableNs": "t0",
              "dataTransformation": {
                "sourceFieldName": "_DateTime_EST_"
              }
            },
            "sortDir": 1
          }
        ],
        "includeRowsCount": true,
        "relatedDimensionMask": {
          "addDisplay": false,
          "addUniqueId": false,
          "addLatLong": false
        },
        "paginateInfo": {
          "startRow": 1,
          "rowsCount": 50
        },
        "dsFilterOverrides": [],
        "filters": [
          {
            "filterDefinition": {
              "filterExpression": {
                "include": false,
                "conceptType": 0,
                "concept": {
                  "ns": "t0",
                  "name": "qt_exs3vib9qc"
                },
                "filterConditionType": "PT",
                "stringValues": [
                  "Shoulder"
                ],
                "numberValues": [],
                "queryTimeTransformation": {
                  "dataTransformation": {
                    "sourceFieldName": "_Source_Type_"
                  }
                }
              }
            },
            "dataSubsetNs": {
              "datasetNs": "d0",
              "tableNs": "t0",
              "contextNs": "c0"
            },
            "version": 3
          },
          {
            "filterDefinition": {
              "filterExpression": {
                "include": true,
                "conceptType": 0,
                "concept": {
                  "name": "qt_kjyfx83arc",
                  "ns": "t0"
                },
                "queryTimeTransformation": {
                  "dataTransformation": {
                    "sourceFieldName": "_County_Name_"
                  }
                },
                "filterConditionType": "IN",
                "stringValues": [
                  "Boyd",
                  "Carter",
                  "Greenup"
                ]
              }
            },
            "dataSubsetNs": {
              "datasetNs": "d0",
              "tableNs": "t0",
              "contextNs": "c0"
            },
            "version": 3,
            "isCanvasFilter": true
          }
        ],
        "features": [],
        "dateRanges": [],
        "contextNsCount": 1,
        "calculatedField": [],
        "needGeocoding": false,
        "geoFieldMask": [],
        "multipleGeocodeFields": [],
        "timezone": "America/New_York"
      },
      "role": "main",
      "retryHints": {
        "useClientControlledRetry": true,
        "isLastRetry": false,
        "retryCount": 0,
        "originalRequestId": "cd-rdkny9a9qc_0_0"
      }
    },
    {
      "requestContext": {
        "reportContext": {
          "reportId": "1413fcfb-1416-4e56-8967-55f8e9f30ec8",
          "pageId": "p_pbm4eo88qc",
          "mode": 1,
          "componentId": "cd-hgodhfhbrc",
          "displayType": "dimension-filter",
          "actionId": "crossFilters"
        },
        "requestMode": 7
      },
      "datasetSpec": {
        "dataset": [
          {
            "datasourceId": "c2fc8cdd-46bb-454c-bf09-90ebfd4067d7",
            "revisionNumber": 0,
            "parameterOverrides": []
          }
        ],
        "queryFields": [
          {
            "name": "qt_vwmdhfhbrc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_Incident_Source_"
            }
          },
          {
            "name": "qt_d5jjfyu6wc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "datastudio_record_count_system_field_id_98323387"
            }
          }
        ],
        "sortData": [
          {
            "sortColumn": {
              "name": "qt_vwmdhfhbrc",
              "datasetNs": "d0",
              "tableNs": "t0",
              "dataTransformation": {
                "sourceFieldName": "_Incident_Source_"
              }
            },
            "sortDir": 0
          }
        ],
        "includeRowsCount": true,
        "relatedDimensionMask": {
          "addDisplay": false,
          "addUniqueId": false,
          "addLatLong": false
        },
        "paginateInfo": {
          "startRow": 1,
          "rowsCount": 5001
        },
        "dsFilterOverrides": [],
        "filters": [
          {
            "filterDefinition": {
              "filterExpression": {
                "include": true,
                "conceptType": 0,
                "concept": {
                  "name": "qt_kjyfx83arc",
                  "ns": "t0"
                },
                "queryTimeTransformation": {
                  "dataTransformation": {
                    "sourceFieldName": "_County_Name_"
                  }
                },
                "filterConditionType": "IN",
                "stringValues": [
                  "Boyd",
                  "Carter",
                  "Greenup"
                ]
              }
            },
            "dataSubsetNs": {
              "datasetNs": "d0",
              "tableNs": "t0",
              "contextNs": "c0"
            },
            "version": 3,
            "isCanvasFilter": true
          }
        ],
        "features": [],
        "dateRanges": [],
        "contextNsCount": 1,
        "dateRangeDimensions": [
          {
            "name": "qt_ilkdhfhbrc",
            "datasetNs": "d0",
            "tableNs": "t0",
            "dataTransformation": {
              "sourceFieldName": "_Incident_Begin_Time_",
              "transformationConfig": {
                "transformationType": 5
              }
            }
          }
        ],
        "calculatedField": [],
        "needGeocoding": false,
        "geoFieldMask": [],
        "multipleGeocodeFields": [],
        "timezone": "America/New_York"
      },
      "role": "main",
      "retryHints": {
        "useClientControlledRetry": true,
        "isLastRetry": false,
        "retryCount": 0,
        "originalRequestId": "cd-hgodhfhbrc_0_0"
      }
    }
  ]
}
"""
payload = json.loads(body_string)

# 7. Send the POST request using the session
try:
    print(f"Sending POST request to {url}...")
    response = session.post(url, headers=headers, json=payload)

    # Raise an exception for bad status codes (4xx or 5xx)
    response.raise_for_status()

    # 8. Process the response
    print(f"Status Code: {response.status_code}")

    # Attempt to print the response as JSON, fall back to text if decoding fails
    try:
        print(response.text)
        print("Response JSON:")
        # Use json.dumps for pretty printing the received JSON
        print(json.dumps(response.json(), indent=2))
    except json.JSONDecodeError:
        print("Response Text (non-JSON):")
        print(response.text)

except requests.exceptions.RequestException as e:
    print(f"An error occurred during the request: {e}")
    if hasattr(e, 'response') and e.response is not None:
        print(f"Response status code: {e.response.status_code}")
        print(f"Response text: {e.response.text}")