Notebooks >> Scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

324 lines
9.0 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2bec1b04-f1ab-4165-a4ea-cc890cc9e5ec",
"metadata": {},
"outputs": [],
"source": [
"pip install gql"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dca88210-18db-45ee-9949-7010b92dbae7",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"import pprint\n",
"\n",
"import requests\n",
"from gql import Client, gql\n",
"from gql.transport.requests import RequestsHTTPTransport\n",
"\n",
"cwd = os.getcwd()\n",
"print(cwd)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "7374f89e-23f8-407d-967b-c76e7d6d93a2",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"active_workflow\n",
"automaticmode/active_workflow\n"
]
}
],
"source": [
"data_json = os.env(PATH)\n",
"\n",
"\n",
"with open(data_json) as f:\n",
" scrubbed_records = json.load(f)\n",
"\n",
"# print(scrubbed_records[0])\n",
"item = scrubbed_records[0]\n",
"name = item[\"Name\"]\n",
"image = item[\"Config\"][\"Image\"]\n",
"# print(name)\n",
"# print(image)\n",
"image = image.replace(\":latest\", \"\")\n",
"name = name.replace(\"/\", \"\")\n",
"print(name)\n",
"print(image)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0367eee-e052-4184-b016-8ecb0dd1aa32",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"for x in scrubbed_records:\n",
" # pprint.pprint(x)\n",
" image = image.replace(\":latest\", \"\")\n",
" name = name.replace(\"/\", \"\")\n",
" data = x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2b300a9-f7c0-4863-9696-29e92e6cedc8",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"for x in scrubbed_records:\n",
" # pprint.pprint(x)\n",
" item = x\n",
" name = item[\"Name\"]\n",
" image = item[\"Config\"][\"Image\"]\n",
" image = image.replace(\":latest\", \"\")\n",
" name = name.replace(\"/\", \"\")\n",
" print(name)\n",
" print(image)\n",
" input_set = {\"id\": name, \"image\": image, \"name\": name, \"raw\": item}\n",
"\n",
" my_headers = {\"Authorization\": \"os.env(directus_token)\", \"Content-Type\": \"application/json\"}\n",
" response = requests.post(\n",
" \"https://cms.donavanaldrich.com/items/containers\",\n",
" headers=my_headers,\n",
" json=(input_set),\n",
" )\n",
" print(response.json())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "927fe89f-9660-4681-884b-f258fc669b88",
"metadata": {},
"outputs": [],
"source": [
"pip install gql[all] aiohttp\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "a4dc5471-3df9-4d52-865e-ca9202d472a3",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'RequestsHTTPTransport' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_7244/3689489481.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m sample_transport = RequestsHTTPTransport(\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"http://directus:8055/graphql\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0muse_json\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m headers={\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\"Content-type\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"application/json\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'RequestsHTTPTransport' is not defined"
]
}
],
"source": [
"\n",
"sample_transport = RequestsHTTPTransport(\n",
" url=\"http://directus:8055/graphql\",\n",
" use_json=True,\n",
" headers={\n",
" \"Content-type\": \"application/json\",\n",
" \"Authorization\": \"os.env(directus_token)\",\n",
" },\n",
" verify=True,\n",
" retries=3,\n",
")\n",
"\n",
"client = Client(\n",
" transport=sample_transport,\n",
" fetch_schema_from_transport=True,\n",
")\n",
"\n",
"query = gql(\n",
" '''\n",
" query {\n",
" pages {\n",
" single(id: 3) {\n",
" id\n",
" path\n",
" locale\n",
" title\n",
" description\n",
" contentType\n",
" isPublished\n",
" isPrivate\n",
" privateNS\n",
" createdAt\n",
" updatedAt\n",
" }\n",
" }\n",
" }\n",
"'''\n",
")\n",
"\n",
"# params = { \"title\": article_attributes_data-posted-on }\n",
"\n",
"result = client.execute(query, variable_values=params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48935cc6-c7c7-43f7-9fb7-c34bd457c903",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"import asyncio\n",
"\n",
"from gql import gql, Client\n",
"from gql.transport.aiohttp import AIOHTTPTransport\n",
"\n",
"transport = AIOHTTPTransport(\n",
" url=\"http://wiki:3000/graphql\",\n",
" headers={\n",
" \"Content-type\": \"application/json\",\n",
" \"Authorization\": \"os.env(directus_token)\",\n",
" },\n",
")\n",
"\n",
"# Create a GraphQL client using the defined transport\n",
"# client = Client(transport=transport, fetch_schema_from_transport=True)\n",
"client = Client(transport=transport, fetch_schema_from_transport=False)\n",
"# client = Client(transport=transport, fetch_schema_from_transport=True)\n",
"\n",
"query = gql(\n",
" '''\n",
" query pageList {\n",
" pages {\n",
" list(locale: \"en\") {\n",
" id\n",
" path\n",
" locale\n",
" title\n",
" description\n",
" contentType\n",
" isPublished\n",
" isPrivate\n",
" privateNS\n",
" createdAt\n",
" updatedAt\n",
" tags\n",
" }\n",
" }\n",
" }\n",
"'''\n",
")\n",
"\n",
"\n",
"\n",
"# result = await session.execute(query)\n",
"# print(result)\n",
"result = await client.execute_async(query)\n",
"print(result)\n",
"\n",
" \n",
"# asyncio.run(main())\n",
"# Execute the query on the transport\n",
"# result = await client.execute_async(query)\n",
"# print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfe2738f-7b10-45e2-bb31-38e8820b1ed6",
"metadata": {},
"outputs": [],
"source": [
"from gql import gql, Client\n",
"from gql.transport.aiohttp import AIOHTTPTransport\n",
"\n",
"# Select your transport with a defined url endpoint\n",
"transport = AIOHTTPTransport(url=\"https://countries.trevorblades.com/\")\n",
"\n",
"# Create a GraphQL client using the defined transport\n",
"client = Client(transport=transport, fetch_schema_from_transport=True)\n",
"\n",
"# Provide a GraphQL query\n",
"query = gql(\n",
" \"\"\"\n",
" query getContinents {\n",
" continents {\n",
" code\n",
" name\n",
" }\n",
" }\n",
"\"\"\"\n",
")\n",
"\n",
"# Execute the query on the transport\n",
"result = client.execute(query)\n",
"print(result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.9 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"toc-autonumbering": false,
"toc-showcode": false,
"toc-showtags": false,
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}