Skip to content

Commit

Permalink
new repos upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 28, 2017
1 parent 3580dc5 commit 58c53ab
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

from . import app
from . import base
from . import upload

from .app import ReposApp
from .base import get_api
54 changes: 54 additions & 0 deletions src/examples/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Prismic API
# Copyright (c) 2008-2017 Hive Solutions Lda.
#
# This file is part of Hive Prismic API.
#
# Hive Prismic API is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Prismic API is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Prismic API. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2017 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

from . import base

if __name__ == "__main__":
api = base.get_api()
api.publish_package(
"hello",
"test",
b"hello world",
identifier = "pt.hive.hello",
info = dict(message = "hello world"),
type = "text",
content_type = "text/plain",
)
else:
__path__ = []
41 changes: 41 additions & 0 deletions src/repos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,49 @@ def __init__(self, *args, **kwargs):
self.base_url = kwargs.get("base_url", self.base_url)
self.username = kwargs.get("username", self.username)
self.password = kwargs.get("password", self.password)
self.session_id = kwargs.get("session_id", None)
self._build_url()

def build(
self,
method,
url,
data = None,
data_j = None,
data_m = None,
headers = None,
params = None,
mime = None,
kwargs = None
):
auth = kwargs.pop("auth", True)
if auth: params["sid"] = self.get_session_id()

def get_session_id(self):
if self.session_id: return self.session_id
return self.login()

def auth_callback(self, params, headers):
self.session_id = None
session_id = self.get_session_id()
params["session_id"] = session_id

def login(self, username = None, password = None):
username = username or self.username
password = password or self.password
url = self.base_url + "api/admin/login"
contents = self.post(
url,
callback = False,
auth = False,
username = username,
password = password
)
self.session_id = contents.get("session_id", None)
self.tokens = contents.get("tokens", None)
self.trigger("auth", contents)
return self.session_id

def _build_url(self):
if not self.username: return
if not self.password: return
Expand Down
35 changes: 32 additions & 3 deletions src/repos/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,48 @@
__license__ = "Apache License, Version 2.0"
""" The license for the module """

import json

import appier

class PackageAPI(object):

def list_packages(self, *args, **kwargs):
url = self.base_url + "packages"
contents = self.get(url, **kwargs)
contents = self.get(url, auth = False, **kwargs)
return contents

def retrieve_package(self, name, version):
url = self.base_url + "packages/%s" % name
contents = self.get(url, version = version)
contents = self.get(url, version = version, auth = False)
return contents

def publish_package(
self,
name,
version,
contents,
identifier = None,
info = None,
type = None,
content_type = None
):
url = self.base_url + "packages"
contents = self.post(
url,
data_m = dict(
name = name,
version = version,
contents = appier.FileTuple.from_data(contents),
identifier = identifier,
info = json.dumps(info),
type = type,
content_type = content_type
)
)
return contents

def info_package(self, name, version = None):
url = self.base_url + "packages/%s/info" % name
contents = self.get(url, version = version)
contents = self.get(url, version = version, auth = False)
return contents

0 comments on commit 58c53ab

Please sign in to comment.