first
This commit is contained in:
55
piapi_ai/Entity.py
Normal file
55
piapi_ai/Entity.py
Normal file
@ -0,0 +1,55 @@
|
||||
from urllib.error import HTTPError
|
||||
import jsonschema
|
||||
import json
|
||||
import requests
|
||||
|
||||
|
||||
class Entity:
|
||||
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.endpoint = config['PIAPI_URL']
|
||||
self.token = config['PIAPI_TOKEN']
|
||||
|
||||
def raw_request(self, method, params=None, data=None, headers=None):
|
||||
if data is None:
|
||||
data = {}
|
||||
if params is None:
|
||||
params = {}
|
||||
if headers is None:
|
||||
headers = {}
|
||||
headers['X-API-Key'] = self.token
|
||||
|
||||
try:
|
||||
response = requests.request(method=method,
|
||||
url="{url}".format(url=self.endpoint),
|
||||
params=params,
|
||||
json=data,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
except HTTPError as http_err:
|
||||
print(f'HTTP error occurred: {http_err}') # Python 3.6
|
||||
except Exception as err:
|
||||
print(f'Other error occurred: {err}') # Python 3.6
|
||||
else:
|
||||
json_response = response.json()
|
||||
|
||||
return json_response
|
||||
|
||||
def validate_dict(self, dictionary, schema):
|
||||
# schema = {
|
||||
# 'type': 'object',
|
||||
# 'properties': {
|
||||
# 'key1': {'type': 'integer'},
|
||||
# 'key2': {'type': 'string'}
|
||||
# },
|
||||
# 'required': ['key1', 'key2']
|
||||
# }
|
||||
try:
|
||||
jsonschema.validate(dictionary, schema)
|
||||
return True
|
||||
except jsonschema.ValidationError as ex:
|
||||
print(ex)
|
||||
return False
|
6
piapi_ai/PiapiAiApi.py
Normal file
6
piapi_ai/PiapiAiApi.py
Normal file
@ -0,0 +1,6 @@
|
||||
from piapi_ai.entities.Flux import Flux
|
||||
|
||||
class PiapiAiApi:
|
||||
|
||||
def __init__(self, config):
|
||||
self.flux = Flux(config)
|
8
piapi_ai/__init__.py
Normal file
8
piapi_ai/__init__.py
Normal file
@ -0,0 +1,8 @@
|
||||
import os
|
||||
from dotenv import load_dotenv, dotenv_values
|
||||
|
||||
from piapi_ai.PiapiAiApi import PiapiAiApi
|
||||
|
||||
config = dotenv_values('.env')
|
||||
|
||||
client = PiapiAiApi(config)
|
15
piapi_ai/entities/Flux.py
Normal file
15
piapi_ai/entities/Flux.py
Normal file
@ -0,0 +1,15 @@
|
||||
from piapi_ai.Entity import Entity
|
||||
|
||||
|
||||
class Flux(Entity):
|
||||
|
||||
def create_task(self, data):
|
||||
result = self.raw_request("post", data=data)
|
||||
|
||||
return result
|
||||
|
||||
def get_task(self, task_id):
|
||||
self.endpoint = self.endpoint + "/" + task_id
|
||||
result = self.raw_request("get")
|
||||
|
||||
return result
|
Reference in New Issue
Block a user