Skip to content

Commit

Permalink
Merge pull request #1 from RusherRG/master
Browse files Browse the repository at this point in the history
Add API calls for APA Pipeline Model and Handler
  • Loading branch information
KamalGalrani authored Jun 10, 2019
2 parents b272b16 + 99b5267 commit 1a8c629
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 3 deletions.
3 changes: 3 additions & 0 deletions grafana_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
from .search import Search
from .user import User, Users
from .team import Teams
from .pipelines import Pipeline
from .handler import Handler
from .model import Model
21 changes: 21 additions & 0 deletions grafana_api/api/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,24 @@ def delete_datasource_by_name(self, datasource_name):
delete_datasource = '/datasources/name/%s' % datasource_name
r = self.api.DELETE(delete_datasource)
return r

def upload_datasource_files(self, datasource_id, filename):
"""
:param datasource_id:
:return:
"""
upload_datasource = '/datasources/%d/files' % datasource_id
files = {'file': open(filename, 'rb')}
r = self.api.POST(upload_datasource, files=files)
return r

def delete_datasource_file(self, datasource_id, file_id):
"""
:param datasource_id:
:return:
"""
upload_datasource = '/datasources/%d/files/%d' % (datasource_id, file_id)
r = self.api.DELETE(upload_datasource)
return r
75 changes: 75 additions & 0 deletions grafana_api/api/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from .base import Base


class Handler(Base):
def __init__(self, api):
super(Handler, self).__init__(api)
self.api = api
self.path = '/org/handlers'

def create_handler(self, name, handler_type, data, description=""):
if type(name) != str or not name:
raise ValueError("Handler name is required!")
if type(description) != str:
raise ValueError("Handler description has to be string!")
if handler_type not in ("fetch", "batch", "step"):
raise ValueError("Handler type '%s' is invalid!" % str(handler_type))
if type(data) != str or not data:
raise ValueError("Handler data is required!")

response = self.api.POST(self.path, json=dict(
name=name,
description=description,
type=handler_type,
data=data,
))
return response

def update_handler(self, id, name, data, description="", **kwargs):
if type(id) != int or not id:
raise ValueError("Handler id is invalid!")
if type(name) != str or not name:
raise ValueError("Handler name is invalid!")
if type(description) != str:
raise ValueError("Handler description has to be string!")
if type(data) != str or not data:
raise ValueError("Handler data is invalid!")

response = self.api.PUT(self.path + "/%d" % id, json=dict(
name=name,
description=description,
data=data,
))
return response


def list_handlers(self, handler_type="", limit=100, page=0):
if handler_type not in ("fetch", "batch", "step", ""):
raise ValueError("Handler type '%s' is invalid!" % str(handler_type))
if type(limit) != int or limit < 0:
raise ValueError("limit is invalid!")
if type(page) != int or page < 0:
raise ValueError("page is invalid!")

response = self.api.GET(self.path, json=dict(
type=handler_type,
limit=limit,
page=page
))
return response

def get_handler(self, id, depends=False):
if type(id) != int or not id:
raise ValueError("Handler id is invalid!")

response = self.api.GET(self.path + "/%d" % id, json=dict(
depends=depends
))
return response

def delete_handler(self, id):
if type(id) != int or not id:
raise ValueError("Handler id is invalid!")

response = self.api.DELETE(self.path + "/%d" % id)
return response
62 changes: 62 additions & 0 deletions grafana_api/api/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from .base import Base


class Model(Base):
def __init__(self, api):
super(Model, self).__init__(api)
self.api = api
self.path = '/org/models'

def create_model(self, name, description=""):
if type(name) != str or not name:
raise ValueError("Model name is required!")
if type(description) != str:
raise ValueError("Model description has to be string!")

response = self.api.POST(self.path, json=dict(
name=name,
description=description
))
return response

def update_model(self, id, name, description="", **kwargs):
if type(id) != int or not id:
raise ValueError("Model id is invalid!")
if type(name) != str or not name:
raise ValueError("Model name is invalid!")
if type(description) != str:
raise ValueError("Model description has to be string!")

response = self.api.PUT(self.path + "/%d" % id, json=dict(
name=name,
description=description
))
return response

def list_models(self, limit=100, page=0):
if type(limit) != int or limit < 0:
raise ValueError("limit is invalid!")
if type(page) != int or page < 0:
raise ValueError("page is invalid!")

response = self.api.GET(self.path, json=dict(
limit=limit,
page=page
))
return response

def get_model(self, id, depends=False):
if type(id) != int or not id:
raise ValueError("Model id is invalid!")

response = self.api.GET(self.path + "/%d" % id, json=dict(
depends=depends
))
return response

def delete_model(self, id):
if type(id) != int or not id:
raise ValueError("Model id is invalid!")

response = self.api.DELETE(self.path + "/%d" % id)
return response
74 changes: 74 additions & 0 deletions grafana_api/api/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from .base import Base


class Pipeline(Base):
def __init__(self, api):
super(Pipeline, self).__init__(api)
self.api = api
self.path = '/org/pipelines'

def create_pipeline(self, name, pipeline_type, data, description=""):
if type(name) != str or not name:
raise ValueError("Pipeline name is required!")
if type(description) != str:
raise ValueError("Pipeline description has to be string!")
if pipeline_type not in ("fetch", "analyse", "tune", "fit", "predict"):
raise ValueError("Pipeline type '%s' is invalid!" % str(pipeline_type))
if type(data) != str or not data:
raise ValueError("Pipeline data is required!")

response = self.api.POST(self.path, json=dict(
name=name,
description=description,
type=pipeline_type,
data=data,
))
return response

def update_pipeline(self, id, name, data, description="", **kwargs):
if type(id) != int or not id:
raise ValueError("Pipeline id is invalid!")
if type(name) != str or not name:
raise ValueError("Pipeline name is invalid!")
if type(description) != str:
raise ValueError("Pipeline description has to be string!")
if type(data) != str or not data:
raise ValueError("Pipeline data is invalid!")

response = self.api.PUT(self.path + "/%d" % id, json=dict(
name=name,
description=description,
data=data,
))
return response

def list_pipelines(self, pipeline_type="", limit=100, page=0):
if pipeline_type not in ("fetch", "analyse", "tune", "fit", "predict", ""):
raise ValueError("Pipeline type '%s' is invalid!" % str(pipeline_type))
if type(limit) != int or limit < 0:
raise ValueError("limit is invalid!")
if type(page) != int or page < 0:
raise ValueError("page is invalid!")

response = self.api.GET(self.path, json=dict(
type=pipeline_type,
limit=limit,
page=page
))
return response

def get_pipeline(self, id, depends=False):
if type(id) != int or not id:
raise ValueError("Pipeline id is invalid!")

response = self.api.GET(self.path + "/%d" % id, json=dict(
depends=depends,
))
return response

def delete_pipeline(self, id):
if type(id) != int or not id:
raise ValueError("Pipeline id is invalid!")

response = self.api.DELETE(self.path + "/%d" % id)
return response
4 changes: 2 additions & 2 deletions grafana_api/grafana_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def construct_api_url():
self.auth = requests.auth.HTTPBasicAuth(*self.auth)

def __getattr__(self, item):
def __request_runnner(url, json=None, headers=None):
def __request_runnner(url, json=None, headers=None, files=None):
__url = '%s%s' % (self.url, url)
runner = getattr(self.s, item.lower())
r = runner(__url, json=json, headers=headers, auth=self.auth, verify=self.verify)
r = runner(__url, json=json, headers=headers, auth=self.auth, verify=self.verify, files=files)

if 500 <= r.status_code < 600:
raise GrafanaServerError("Server Error {0}: {1}".format(r.status_code,
Expand Down
5 changes: 4 additions & 1 deletion grafana_api/grafana_face.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .grafana_api import GrafanaAPI
from .api import (Admin, Dashboard, Datasource, Folder, Organization, Organizations, Search, User, Users, Teams)
from .api import (Admin, Dashboard, Datasource, Folder, Organization, Organizations, Search, User, Users, Teams, Pipeline, Handler, Model)


class GrafanaFace:
Expand All @@ -15,3 +15,6 @@ def __init__(self, auth, host="localhost", port=None, url_path_prefix="", protoc
self.user = User(self.api)
self.users = Users(self.api)
self.teams = Teams(self.api)
self.pipelines = Pipeline(self.api)
self.model = Model(self.api)
self.handler = Handler(self.api)

0 comments on commit 1a8c629

Please sign in to comment.