Ejemplo n.º 1
0
def zebra_alert_handler(socket, address):
    try:
        fileobj = socket.makefile()

        while True:
            line = fileobj.readline()
            if not line:
                break
            fileobj.write(line)
            fileobj.flush()
            raw_msg = line.strip()
            message_split = raw_msg.split(':')
            if message_split[0] == 'ALERT' or message_split[0] == 'ERROR CONDITION':
                #it's an alert/error,
                is_cleared=False
            elif message_split[0] == 'ALERT CLEARED' or message_split[0] == 'ERROR CLEARED':
                is_cleared=True
            else:
                is_cleared=True
            condition = message_split[1].split('[')[0].strip().lower()


            printer_uri = identify_printer(address, printers)

            res = Resource(celeryconfig.SERVER_HOST)
            auth_params = {'username':celeryconfig.ZPRINTER_USERNAME, 'api_key': celeryconfig.ZPRINTER_API_KEY}
            #r = res.get('/api/zebra_status/',  params_dict=auth_params)
            new_instance = dict()
            new_instance['printer'] = printer_uri
            new_instance['event_date'] =  datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000")
            new_instance['status'] = condition
            new_instance['is_cleared'] = is_cleared
            res.post('api/zebra_status/', simplejson.dumps(new_instance), headers={'Content-Type': 'application/json'}, params_dict=auth_params)
    except Exception, ex:
        logging.error("Unknown exception, gobbling up: %s", ex)
Ejemplo n.º 2
0
def post_authenticated_data(data, url, username, password, headers=None):
    """
    Post basic authenticated data, using restkit
    """ 
    auth = BasicAuth(username, password)
    r = Resource(url, filters=[auth, ])
    if headers is not None:
        return (r.post(payload=data, headers=headers).body_string(), None)
    return (r.post(payload=data).body_string(), None)
Ejemplo n.º 3
0
def post_authenticated_data(data, url, username, password, headers=None):
    """
    Post basic authenticated data, using restkit
    """
    auth = BasicAuth(username, password)
    r = Resource(url, filters=[
        auth,
    ])
    if headers is not None:
        return (r.post(payload=data, headers=headers).body_string(), None)
    return (r.post(payload=data).body_string(), None)
Ejemplo n.º 4
0
def post_unauthenticated_data(data, url):
    """
    Post basic unauthenticated data, using restkit instead of the post_data
    method.
    """
    r = Resource(url, filters=[])
    return r.post(payload=data).body_string(), None
Ejemplo n.º 5
0
def _post_payload(host, path, data, **kwargs):
    """generic function to handle posting data
    :rtype : return body of page
    :param host: host to send data to
    :param path: path to interact with
    :param data: data to send
    :param kwargs:  additional keyword args
    """

    if "http://" not in host:
        host = "http://{0}".format(host)

    res = Resource(host)

    payload = "jsonParameters={0}".format(json.dumps(data))
    for key, value in kwargs.items():
        payload += '&{0}={1}'.format(key, json.dumps(value))
    headers = {"Content-Type": "application/json"}
    # print "path is %s" % path
    page = res.post(path=path, payload=payload, headers=headers)
    body = page.body_string()
    if body:
        body = json.loads(body)

        if isinstance(body, dict) and "ERROR" in body:
            raise HelixException(body["ERROR"])

    # test what was returned, see if any exceptions need to be raise
    # if not body:
    # raise HelixException("body for path {0} is empty".format(path))
    # else:
    # print "BODY IS EMPTY FOR ", path
    # print "BODY is %s." % body

    return body
Ejemplo n.º 6
0
class Query(object):
    def __init__(self,
                 url=GITHUB_URL,
                 params=None,
                 payload=None,
                 headers=None,
                 filters=None,
                 access_token=None):
        self.url = url
        self.params = params or dict()
        self.payload = payload or dict()
        self.headers = headers or {'Content-Type': 'application/json'}
        filters = filters or list()
        self.resource = Resource(
            url,
            pool=ConnectionPool(factory=Connection),
            filters=filters,
        )
        if access_token is not None:
            self.params["access_token"] = access_token

    def concat_path(self, *args):
        for path in args:
            self.resource.update_uri(path)

    def do_GET(self, path=None, params=None):
        params = params or self.params
        response = self.resource.get(path, self.headers, params)
        return self.parse_response(response.body_string())

    def do_POST(self, path=None, payload=None, params=None):
        payload = payload or self.payload
        params = params or self.params
        response = self.resource.post(path, json.dumps(payload), self.headers,
                                      params)
        return self.parse_response(response.body_string())

    def do_DELETE(self, path=None, params=None):
        params = params or self.params
        response = self.resource.delete(path, self.headers, params)
        return self.parse_response(response.body_string())

    def do_PATCH(self, path=None, payload=None, params=None):
        payload = payload or self.payload
        params = params or self.params
        response = self.resource.request("PATCH", path, json.dumps(payload),
                                         self.headers, params)
        return self.parse_response(response.body_string())

    def parse_response(self, response):
        try:
            return json.loads(response)
        except:
            return response

    def __repr__(self):
        return "uri:<{0}>".format(self.resource.uri)

    def __str__(self):
        return self.resource.uri
Ejemplo n.º 7
0
    def _post_payload(self, path, data, **kwargs):
        """generic function to handle posting data
        :rtype : return body of page
        :param path: path to interact with
        :param data: data to send
        :param kwargs:  additional keyword args
        """

        res = Resource(self.host)

        payload = "jsonParameters={0}".format(json.dumps(data))
        for key, value in kwargs.items():
            payload += '&{0}={1}'.format(key, json.dumps(value))
        headers = {"Content-Type": "application/json"}
        # print "path is %s" % path
        page = res.post(path=path, payload=payload, headers=headers)
        body = page.body_string()
        if body:
            body = json.loads(body)

            if isinstance(body, dict) and "ERROR" in body:
                raise HelixException(body["ERROR"])

        # test what was returned, see if any exceptions need to be raise
        # if not body:
        # raise HelixException("body for path {0} is empty".format(path))
        # else:
        # print "BODY IS EMPTY FOR ", path
        # print "BODY is %s." % body

        return body
Ejemplo n.º 8
0
def post_unauthenticated_data(data, url):
    """
    Post basic unauthenticated data, using restkit instead of the post_data
    method.
    """
    r = Resource(url, filters=[])
    return r.post(payload=data).body_string(), None
Ejemplo n.º 9
0
    def _run(self, action, headers=None, **kwargs):
        """
        Requests API
        """
        res = Resource(self.HOST)
        payload = kwargs
        payload.update({'action': action, 'api_token': self.api_token})
        response = res.post(payload=payload, headers=headers)

        if response.status_int != 200:
            raise POEditorException(
                status='fail',
                error_code=response.status_int,
                message=response.status
            )

        data = json.loads(response.body_string().decode('utf-8'))

        if 'response' not in data:
            raise POEditorException(
                status='fail',
                error_code=-1,
                message=u'"response" key is not present'
            )

        if 'status' in data['response'] and \
                data['response']['status'] != self.SUCCESS_CODE:
            raise POEditorException(
                error_code=data['response'].get('code'),
                status=data['response']['status'],
                message=data['response'].get('message')
            )

        return data
Ejemplo n.º 10
0
    def _run(self, action, headers=None, **kwargs):
        """
        Requests API
        """
        res = Resource(self.HOST)
        payload = kwargs
        payload.update({'action': action, 'api_token': self.api_token})
        response = res.post(payload=payload, headers=headers)

        if response.status_int != 200:
            raise POEditorException(
                status='fail',
                error_code=response.status_int,
                message=response.status
            )

        data = json.loads(response.body_string().decode('utf-8'))

        if 'response' not in data:
            raise POEditorException(
                status='fail',
                error_code=-1,
                message=u'"response" key is not present'
            )

        if 'status' in data['response'] and \
                data['response']['status'] != self.SUCCESS_CODE:
            raise POEditorException(
                error_code=data['response'].get('code'),
                status=data['response']['status'],
                message=data['response'].get('message')
            )

        return data
Ejemplo n.º 11
0
def collect_from_zigbee(target, stream_id, device=DEFAULT_SERIAL_DEVICE, baud_rate=DEFAULT_BAUD_RATE):
    """Synchronously read from the serial port to target

    target can either be the URL of a couchdb server or a restkit resource
    instance.
    """
    from xbee import ZigBee

    if not hasattr(target, "post"):
        target = Resource(target)

    serial_device = serial.Serial(device, baud_rate)
    headers = {"Content-Type": "application/json"}
    if stream_id is None:
        stream_prefix = ""
    else:
        stream_prefix = stream_id + "_"
    try:
        xbee = ZigBee(serial_device, escaped=True)
        # Continuously read and print packets
        while True:
            try:
                response = xbee.wait_read_frame()
                now = datetime.datetime.utcnow()
                timestamp = now.replace(microsecond=0).isoformat() + "Z"
                logging.debug(response)
                samples = response.get("samples", [])
                for sample in samples:
                    for sensor_id, sensor_value in sample.iteritems():
                        target.post(
                            headers=headers,
                            payload=json.dumps(
                                {
                                    # TODO: use a mapping mechanism instead of stream_id
                                    # prefix
                                    "datastream_id": stream_prefix + sensor_id,
                                    "value": sensor_value,
                                    "timestamp": timestamp,
                                }
                            ),
                        )

            except KeyboardInterrupt:
                break
    finally:
        serial_device.close()
Ejemplo n.º 12
0
 def create_token(self, user, password):
     serverurl = "https://api.github.com"
     auth = BasicAuth(user, password)
     authreqdata = {"scopes": ["repo"], "note": "admin script"}
     resource = Resource('https://api.github.com/authorizations',
             pool=self.pool, filters=[auth])
     response = resource.post(headers={"Content-Type": "application/json"},
             payload=json.dumps(authreqdata))
     self.token = json.loads(response.body_string())['token']
Ejemplo n.º 13
0
 def create_token(self, user, password):
     serverurl = "https://api.github.com"
     auth = BasicAuth(user, password)
     authreqdata = {"scopes": ["repo"], "note": "admin script"}
     resource = Resource('https://api.github.com/authorizations',
                         pool=self.pool,
                         filters=[auth])
     response = resource.post(headers={"Content-Type": "application/json"},
                              payload=json.dumps(authreqdata))
     self.token = json.loads(response.body_string())['token']
Ejemplo n.º 14
0
 def create_pull_request(self, user, repo, to_user, base_branch,
                         branch, title="", body=""):
     if not title:
         title = "Robot pull request. Please review."
     resource = Resource("https://api.github.com/repos/%s/%s/pulls" %
                         (to_user, repo))
     pulldata = {"title": title, "body": body,
                 "head": "%s:%s" % (user, branch), "base": base_branch}
     response = resource.post(headers=self.headers,
                              payload=json.dumps(pulldata))
     return json.loads(response.body_string())
Ejemplo n.º 15
0
 def _post(self, url, payload=None):
     auth = BasicAuth(self.login, self.password)
     res = Resource(url, filters=[auth])
     headers = {'content-type': 'application/x-www-form-urlencoded'}
     r = res.post(payload=payload, headers=headers)
     if r.status_int == 204: # No content
         return None
     if r['Content-Type'].startswith('application/json'):
         return json.loads(r.body_string())
     else:
         return r.body_string()
Ejemplo n.º 16
0
def get_printer_heartbeat():
    """
    Task to actively monitor the printer's status (vs. waiting to get alerts)
    """
    if len(printer_dict.keys()) == 0:
        get_printers()
    msg_text = """^XA^HH^XZ"""
    for k,v in printer_dict.items():
        print_host = v['ip_address']
        print_port = v['port']
        printer_uri = v['resource_uri']

        info = do_send(print_host, print_port, msg_text, recv=True)

        #prepare the rest resource for sending info to server
        res = Resource(celeryconfig.SERVER_HOST, manager=manager)
        auth_params = {'username':celeryconfig.ZPRINTER_USERNAME, 'api_key': celeryconfig.ZPRINTER_API_KEY}

        new_instance = dict()
        new_instance['printer'] = printer_uri
        new_instance['event_date'] =  datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000")
        new_instance['status'] = 'printer uptime heartbeat'

        if info is None or info == False:
            #failed to reach printer or receive data
            new_instance['is_cleared'] = False
        elif isinstance(info, str):
            #it's a string with info
            if info.count('ZPL MODE') > 0:
                #we got diagnostic info from printer
                new_instance['is_cleared'] = True
            else:
                #something else, error
                new_instance['is_cleared'] = False
        try:
            res.post('api/zebra_status/', simplejson.dumps(new_instance), headers={'Content-Type': 'application/json'}, params_dict=auth_params)
        except Exception, ex:
            logging.error("Error trying to post heartbeat to host %s" % ex)
Ejemplo n.º 17
0
 def submit(self):
     """
     Performs an order (HTTP POST) on the gateway
     """
     if self.total_amount_cur == None:
         total_amount_cur = ""
     else:
         total_amount_cur = self.total_amount_cur
         
     values = {
         'StoreId': self.store_id,
         'Langue': self.langue,
         'OfferURL': self.offer_url,
         'UpdateURL': self.update_url,
         'CartId': self.cart_id,
         'Mode': self.mode,
         'Count': self.count,
         'Desc': self.desc, 
         'Qty': self.qty,
         'ItmesPrices': self.items_price,
         'AmountTX': self.amount_tx,
         'ShippingCharge': self.shipping_charge,
         'ShippingWeight': self.shipping_weight,
         'TotalmountTx': self.total_amount_tx,
         'TotalamountCur':  total_amount_cur,
         'SymbolCur': self.symbol_cur,
         'MultiPayment': self.multi_payment,
         'BuyerName': self.buyer_name,
         'Address': self.address,
         'City': self.city,
         'State': self.state,
         'Country': self.country,
         'Postcode': self.postcode,
         'Tel': self.phone, 
         'Email': self.email,
         'Checksum': self._checksum(),
     }
     
     data = _unicode_urlencode(values)
     res = Resource(self.get_endpoint())
     r = res.post(payload=data, headers={'Content-Type': 'application/x-www-form-urlencoded'})
     return r 
Ejemplo n.º 18
0
 def create_pull_request(self,
                         user,
                         repo,
                         to_user,
                         base_branch,
                         branch,
                         title="",
                         body=""):
     if not title:
         title = "Robot pull request. Please review."
     resource = Resource("https://api.github.com/repos/%s/%s/pulls" %
                         (to_user, repo))
     pulldata = {
         "title": title,
         "body": body,
         "head": "%s:%s" % (user, branch),
         "base": base_branch
     }
     response = resource.post(headers=self.headers,
                              payload=json.dumps(pulldata))
     return json.loads(response.body_string())
Ejemplo n.º 19
0
def createTask(server_base_url, user, password, project, task_summary):
    auth = BasicAuth(user, password)

    resource_name = "issue"
    complete_url = "%s/rest/api/latest/%s/" % (server_base_url, resource_name)
    resource = Resource(complete_url, filters=[auth])

    try:
        data = {
            "fields": {
                "project": {
                    "key": project
                },
                "summary": task_summary,
                "issuetype": {
                    "name": "Task"
                }
            }
        }
        response = resource.post(headers={'Content-Type': 'application/json'},
                                 payload=json.dumps(data))
    except Exception, ex:
        print "EXCEPTION: %s " % ex.msg
        return None
def create_ticket(host_url, auth, ticket_fields):
    """
    Takes all of the necessary information about the server and ticket and submits a POST request
    to the jira server's Rest API to create a ticket.

    :param host_url: The url of the jira server.
    :param auth: Credentials to the account the ticket will be created as.
    :param ticket_fields: a dictionary containing all of the information/data necessary to create the ticket

    :return: jira_issue: The json of the response from the server. This can be used to get information about the created
    ticekt later on.
    """

    # Constructing the URL to POST to
    complete_url = "%s/rest/api/2/issue/" % host_url
    # Create the resource using restkit
    resource = Resource(complete_url, filters=[auth])
    post_headers = {'Content-Type': 'application/json'}

    # Try POSTing to Jira Rest API. If failure, exit and print error message.
    try:
        data = {
            "fields": {
                "project": {
                    "key": ticket_fields['project']
                },
                "summary": ticket_fields['summary'],
                "issuetype": {
                    "name": ticket_fields['issue_type']
                }
            }
        }
        response = resource.post(headers=post_headers, payload=json.dumps(data))
    except Exception, ex:
        print("EXCEPTION: %s " % ex.message)
        return None
Ejemplo n.º 21
0
        msg.replace("\n", "")

        commitList.append([commit["id"], msg])

    issueMessage = "# %s is now failing!#\n\n" % jobName
    issueMessage += "Someone broke jenkins!\n\n"
    issueMessage += "## Guilty Commits ##\n"
    for commit in commitList:
        issueMessage += "* [%s](%s/commit/%s)\n" % (commit[1], githubUrl, commit[0])
    issueMessage += "\n\n## Jenkins Reference ##\n"
    issueMessage += "Build URL: %s\n" % failingBuild["url"]
    issueMessage += "\n\nThanks, \nJenkins :cop:\n\n\n"
    issueMessage += "DMWMJENKINSMAGIC: %s!%s!%s" % (jobName, repo, lastBuild)
    resource = Resource("https://api.github.com/repos/dmwm/%s/issues" % repo, pool=pool)
    issueInfo = {"title": issueTitle, "body": issueMessage, "labels": ["JenkinsFail"]}
    response = resource.post(headers=headers, payload=json.dumps(issueInfo))
    repos = json.loads(response.body_string())
    #
    print issueMessage


sys.exit(0)
{
    "actions": [
        {
            "parameters": [
                {"name": "WMAGENT_VERSION", "value": ""},
                {"name": "SCRAM_ARCH", "value": "slc5_amd64_gcc461"},
                {"name": "branch", "value": "master"},
                {"name": "pathToTest", "value": "test/python"},
                {"name": "unused", "value": ""},
Ejemplo n.º 22
0
def _post_request(url_path, payload):
    auth = BasicAuth('breyten','0116ffc25e28b40c883b6ae8a46a9bd49373e7c5')
    res = Resource('http://toevalofniet.com', filters=[auth])
    headers = ({'Accepted': 'application/json', 'Content‐Type': 'application/json'})
    result = res.post(path='/api/1.0/' + url_path + '/', payload=payload, headers=headers)
    return result
Ejemplo n.º 23
0
class StoreClient(object):

    def __init__(self, endpoint, name, **kwargs):
        if endpoint.endswith('/'):
            endpoint = endpoint.rstrip('/')
        if 'pool' not in kwargs:
            kwargs['pool'] = ConnectionPool(factory=Connection)
        self.json_default = kwargs.get('json_default', json_util.default)
        self.json_object_hook = kwargs.get('json_object_hook', json_util.object_hook)
        self.resource = Resource(endpoint, **kwargs)
        self.name = name

    def create_store(self, store):
        response = self.resource.post("/stores/%s" % store)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def get_stores(self):
        response = self.resource.get("/stores")
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def create_branch(self, store, branch, parent=None):
        path = _build_path(store, "branch", branch)
        params = _build_params(parent=parent)
        response = self.resource.post(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def get_branch(self, store, branch):
        path = _build_path(store, "branch", branch)
        response = self.resource.get(path)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def merge(self, store, source, target='master', author=None, committer=None):
        path = _build_path(store, "merge", source)
        params = _build_params(target=target, author=author, committer=committer)
        response = self.resource.post(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def get(self, store, key=None, shallow=False, branch='master', commit_sha=None):
        path = _entry_path(store, key)
        params = _build_params(shallow=shallow, branch=branch, commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            response_body = response.body_string()
            return json.loads(response_body, object_hook=self.json_object_hook)

    def put(self, store, key, value, flatten_keys=True, branch='master', author=None, committer=None):
        path = _entry_path(store, key)
        payload = json.dumps(value, default=self.json_default)
        flatten_keys = 1 if flatten_keys else 0
        params = _build_params(flatten_keys=flatten_keys, branch=branch, author=author, committer=committer)
        headers = {'Content-Type': 'application/json'}
        response = self.resource.put(path, headers=headers, payload=payload, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def delete(self, store, key, branch='master', author=None, committer=None):
        path = _entry_path(store, key)
        params = _build_params(branch=branch, author=author, committer=committer)
        response = self.resource.delete(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def keys(self, store, key=None, pattern=None, depth=None, filter_by=None, branch='master', commit_sha=None):
        path = _build_path(store, "keys", key)
        params = _build_params(pattern=pattern, depth=depth, filter_by=filter_by, branch=branch, commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def entries(self, store, key=None, pattern=None, depth=None, branch='master', commit_sha=None):
        path = _build_path(store, "entries", key)
        params = _build_params(pattern=pattern, depth=depth, branch=branch, commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)

    def trees(self, store, key=None, pattern=None, depth=None, object_depth=None, branch='master', commit_sha=None):
        path = _build_path(store, "trees", key)
        params = _build_params(pattern=pattern, depth=depth, object_depth=object_depth, branch=branch, commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(), object_hook=self.json_object_hook)
Ejemplo n.º 24
0
from socketpool import ConnectionPool
import getpass
 
pool = ConnectionPool(factory=Connection)
serverurl="https://api.github.com"
print 'Enter your username:'******'https://api.github.com/authorizations', pool=pool, filters=[auth])
response = resource.post(headers={ "Content-Type": "application/json" }, payload=json.dumps(authreqdata))
token = json.loads(response.body_string())['token']
"""
Once you have a token, you can pass that in the Authorization header
You can store this in a cache and throw away the user/password
This is just an example query.  See http://developer.github.com/v3/ 
for more about the url structure
"""
#token = '94038d59a46c5ea1aa4f11626a83cde3e8794668' 
resource = Resource('https://api.github.com/user/repos', pool=pool)
headers = {'Content-Type' : 'application/json' }
headers['Authorization'] = 'token %s' % token
response = resource.get(headers = headers)
repos = json.loads(response.body_string())
for each in repos:
    print each['clone_url']
Ejemplo n.º 25
0
 def update_pull_request(self, user, repo, number, data):
     resource = Resource("https://api.github.com/repos/%s/%s/pulls/%s" %
             (user, repo, number))
     res = resource.post(headers=self.headers,
             payload=json.dumps(data))
     return json.loads(res.body_string())
Ejemplo n.º 26
0
class Client(object):
    """Derek client."""

    def __init__(self, username, password, host=DEFAULT_HOST, port=DEFAULT_PORT):
        """Constructor."""

        auth = BasicAuth(username, password)
        self.resource = Resource("http://%s:%d" % (host, port), filters=[auth])

    def post(self, path, payload=None):
        """Do POST request to Derek."""

        if payload is None:
            payload = {}

        headers = {"content-type": "application/x-www-form-urlencoded"}

        resp = self.resource.post(path=path, payload=dict2qs(payload), headers=headers)
        return resp

    def postjson(self, path, payload=None):
        """Do POST request to Derek and expect JSON in response."""

        if payload is None:
            payload = {}

        headers = {"accept": "application/json", "content-type": "application/x-www-form-urlencoded"}

        resp = self.resource.post(path=path, payload=dict2qs(payload), headers=headers)
        return json.loads(resp.body_string())

    def getjson(self, path, params=None):
        """Do GET request to Derek and expect JSON in response."""

        if params is None:
            params = {}

        headers = {"accept": "application/json"}
        resp = self.resource.get(path=path, headers=headers, **params)
        return json.loads(resp.body_string())

    def download(self, path, out):
        """Download file."""

        resp = self.resource.get(path=path)
        with resp.body_stream() as body:
            with open(out, "wb") as handle:
                for block in body:
                    handle.write(block)

    def upload(self, path, filepath):
        """Upload file to server."""

        with open(filepath) as pfile:
            self.resource.post(path=path, payload=pfile)

    def branch(self, branch_id):
        """Return Branch object."""
        return Branch(self, branch_id)

    def slice(self, slice_id):
        """Return Slice object."""
        return Slice(self, slice_id)

    def user(self, username):
        """Return User object."""
        return User(self, username)

    def repo(self, repo_id):
        """Return repository object."""
        return Repo(self, repo_id)
Ejemplo n.º 27
0
class Query(object):

    def __init__(self, url=GITHUB_URL, params=None, payload=None, headers=None, filters=None, access_token=None):
        self.url = url
        self.params = params or dict()
        self.payload = payload or dict()
        self.headers = headers or {'Content-Type':'application/json'}
        filters = filters or list()
        self.resource = Resource(
                            url,
                            pool=ConnectionPool(factory=Connection),
                            filters=filters,
                            )
        if access_token is not None:
            self.params["access_token"] = access_token

    def concat_path(self, *args):
        for path in args:
            self.resource.update_uri(path)

    def do_GET(self, path=None, params=None):
        params = params or self.params
        response = self.resource.get(
                                path,
                                self.headers,
                                params)
        return self.parse_response(response.body_string())

    def do_POST(self, path=None, payload=None, params=None):
        payload = payload or self.payload
        params = params or self.params
        response = self.resource.post(
                                path,
                                json.dumps(payload),
                                self.headers,
                                params)
        return self.parse_response(response.body_string())

    def do_DELETE(self, path=None, params=None):
        params = params or self.params
        response = self.resource.delete(
                                path,
                                self.headers,
                                params)
        return self.parse_response(response.body_string())

    def do_PATCH(self, path=None, payload=None, params=None):
        payload = payload or self.payload
        params = params or self.params
        response = self.resource.request(
                                "PATCH",
                                path,
                                json.dumps(payload),
                                self.headers,
                                params)
        return self.parse_response(response.body_string())
    
    def parse_response(self, response):
        try:
            return json.loads(response)
        except:
            return response
    
    def __repr__(self):
        return u"uri:<{0}>".format(self.resource.uri)
    
    def __str__(self):
        return self.resource.uri
Ejemplo n.º 28
0
    def save_base(self,
                  raw=False,
                  cls=None,
                  origin=None,
                  force_insert=False,
                  force_update=False,
                  using=None,
                  update_fields=None):
        """
        Does the heavy-lifting involved in saving. Subclasses shouldn't need to
        override this method. It's separate from save() in order to hide the
        need for overrides of save() to pass around internal-only parameters
        ('raw', 'cls', and 'origin').
        """

        assert not (force_insert and force_update)

        record_exists = False

        if cls is None:
            cls = self.__class__
            meta = cls._meta
            if not meta.proxy:
                origin = cls
        else:
            meta = cls._meta

        if origin and not getattr(meta, "auto_created", False):
            signals.pre_save.send(sender=origin, instance=self, raw=raw)

        model_name = str(meta)

        # If we are in a raw save, save the object exactly as presented.
        # That means that we don't try to be smart about saving attributes
        # that might have come from the parent class - we just save the
        # attributes we have been given to the class we have been given.
        # We also go through this process to defer the save of proxy objects
        # to their actual underlying model.
        if not raw or meta.proxy:
            if meta.proxy:
                org = cls
            else:
                org = None
            for parent, field in meta.parents.items():
                # At this point, parent's primary key field may be unknown
                # (for example, from administration form which doesn't fill
                # this field). If so, fill it.
                if field and getattr(
                        self, parent._meta.pk.attname) is None and getattr(
                            self, field.attname) is not None:
                    setattr(self, parent._meta.pk.attname,
                            getattr(self, field.attname))

                self.save_base(cls=parent, origin=org, using=using)

                if field:
                    setattr(self, field.attname,
                            self._get_pk_val(parent._meta))
            if meta.proxy:
                return

        if not meta.proxy:
            pk_val = self._get_pk_val(meta)
            pk_is_set = pk_val is not None

            get_args = {}
            get_args[ROA_ARGS_NAMES_MAPPING.get('FORMAT',
                                                'format')] = ROA_FORMAT
            get_args.update(ROA_CUSTOM_ARGS)

            # Construct Json payload
            serializer = self.get_serializer(self)
            payload = self.get_renderer().render(serializer.data)

            # Add serializer content_type
            headers = get_roa_headers()
            headers.update(self.get_serializer_content_type())

            # check if resource use custom primary key
            if not meta.pk.attname in ['pk', 'id']:
                # consider it might be inserting so check it first
                # @todo: try to improve this block to check if custom pripary key is not None first
                resource = Resource(self.get_resource_url_detail(),
                                    filters=ROA_FILTERS,
                                    **ROA_SSL_ARGS)
                try:
                    response = resource.get(payload=None,
                                            headers=headers,
                                            **get_args)
                except ResourceNotFound:
                    # since such resource does not exist, it's actually creating
                    pk_is_set = False
                except RequestFailed:
                    pk_is_set = False

            if force_update or pk_is_set and not self.pk is None:
                record_exists = True
                resource = Resource(self.get_resource_url_detail(),
                                    filters=ROA_FILTERS,
                                    **ROA_SSL_ARGS)
                try:
                    logger.debug(
                        u"""Modifying : "%s" through %s with payload "%s" and GET args "%s" """
                        % (force_unicode(self), force_unicode(resource.uri),
                           force_unicode(payload), force_unicode(get_args)))
                    response = resource.put(payload=payload,
                                            headers=headers,
                                            **get_args)
                except RequestFailed as e:
                    raise ROAException(e)
            else:
                record_exists = False
                resource = Resource(self.get_resource_url_list(),
                                    filters=ROA_FILTERS,
                                    **ROA_SSL_ARGS)
                try:
                    logger.debug(
                        u"""Creating  : "%s" through %s with payload "%s" and GET args "%s" """
                        % (force_unicode(self), force_unicode(resource.uri),
                           force_unicode(payload), force_unicode(get_args)))
                    response = resource.post(payload=payload,
                                             headers=headers,
                                             **get_args)
                except RequestFailed as e:
                    raise ROAException(e)

            response = force_unicode(
                response.body_string()).encode(DEFAULT_CHARSET)

            data = self.get_parser().parse(StringIO(response))
            serializer = self.get_serializer(data=data)
            if not serializer.is_valid():
                raise ROAException(
                    u'Invalid deserialization for %s model: %s' %
                    (self, serializer.errors))

            obj = self.__class__(**serializer.initial_data)
            try:
                self.pk = int(obj.pk)
            except ValueError:
                self.pk = obj.pk
            self = obj

        if origin:
            signals.post_save.send(sender=origin,
                                   instance=self,
                                   created=(not record_exists),
                                   raw=raw)
Ejemplo n.º 29
0
class WWWcomm:
    def __init__(self,
                 MIDpass,
                 configPath,
                 baseURL,
                 uploadPath,
                 RCstatusPath,
                 MIDname=None):
        # self.baseURL = defaultConfig.baseURL
        # self.password = defaultConfig

        # obtain base configuration
        self.MIDpass = MIDpass
        self.baseURL = baseURL
        self.configPath = configPath
        self.resource = Resource(baseURL, timeout=60)
        # self.upResource = Resource("http://isadoredev1.exotericanalytics.com:5050")
        self.uploadPath = uploadPath
        self.RCstatusPath = RCstatusPath
        self.MIDname = MIDname

    def getConfig(self):
        params_dict = {"mid_pass": self.MIDpass, "ts": "blah"}
        if self.MIDname:
            params_dict["mid_name"] = self.MIDname
        output = self.resource.get(path=self.configPath,
                                   params_dict=params_dict,
                                   verify=False)

        logging.debug("WWW reply status code: " + str(output.status_int) + ".")
        if output.status_int != 200:
            raise MIDconfigDownloadError(output)

        return json.loads(output.body_string())

    def RC_cmd_status(self, control_id, fetched_p):
        """
			"""
        # TODO: check output for errors, don't just rely upon exceptions
        try:
            if fetched_p:
                output = self.resource.put(path=self.RCstatusPath +
                                           str(control_id),
                                           params_dict={
                                               "fetched": 1,
                                               "MID_pass": self.MIDpass
                                           },
                                           verify=False)
            else:
                output = self.resource.put(path=self.RCstatusPath +
                                           str(control_id),
                                           params_dict={
                                               "fetched": 0,
                                               "MID_pass": self.MIDpass
                                           },
                                           verify=False)
            if output.status_int != 204:
                raise MID_RCstatusError(
                    "Failed to inform WWW of successful RC command: " +
                    str(control_id) + ".  Received response status int: " +
                    str(output.status_int))
        except Exception as e:
            raise MID_RCstatusError(
                "Failed to inform WWW of unsuccessful RC command: " +
                str(control_id) + ".  This is the result of exception: " +
                str(e))

    def uploadData(self, paramString):
        # output = self.upResource.get(path=self.uploadPath,
        # 							 params_dict={"data":paramString})
        # NOTE: can remove verify param once we upgrade Python to 2.7.9+
        output = self.resource.post(path=self.uploadPath,
                                    params_dict={"data": paramString},
                                    verify=False)
        output.skip_body()  # to release the connection
        return output

    def uploadReading(self, passwd, readingTime, cmds, errors):
        payload = {
            "mid_pass":
            passwd,
            "datetime":
            readingTime.isoformat(),
            "data":
            json.dumps(
                reduce(lambda x, y: x + y.to_JSON_WWW_data(readingTime), cmds,
                       [])),
            "errors": []
        }
        # TODO: deal with requests-specific exceptions. in particular, the timeout exception.  for now, letting MID.py deal with it.
        # NOTE: can remove verify param once we upgrade Python to 2.7.9+
        logging.debug(payload["data"])
        r = requests.post(self.baseURL + self.uploadPath,
                          data=payload,
                          timeout=60.0,
                          verify=False)
Ejemplo n.º 30
0
    def save_base(self, raw=False, cls=None, origin=None, force_insert=False,
                  force_update=False, using=None, update_fields=None):
        """
        Does the heavy-lifting involved in saving. Subclasses shouldn't need to
        override this method. It's separate from save() in order to hide the
        need for overrides of save() to pass around internal-only parameters
        ('raw', 'cls', and 'origin').
        """

        assert not (force_insert and force_update)

        record_exists = False

        if cls is None:
            cls = self.__class__
            meta = cls._meta
            if not meta.proxy:
                origin = cls
        else:
            meta = cls._meta

        if origin and not getattr(meta, "auto_created", False):
            signals.pre_save.send(sender=origin, instance=self, raw=raw)

        model_name = str(meta)

        # If we are in a raw save, save the object exactly as presented.
        # That means that we don't try to be smart about saving attributes
        # that might have come from the parent class - we just save the
        # attributes we have been given to the class we have been given.
        # We also go through this process to defer the save of proxy objects
        # to their actual underlying model.
        if not raw or meta.proxy:
            if meta.proxy:
                org = cls
            else:
                org = None
            for parent, field in meta.parents.items():
                # At this point, parent's primary key field may be unknown
                # (for example, from administration form which doesn't fill
                # this field). If so, fill it.
                if field and getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
                    setattr(self, parent._meta.pk.attname, getattr(self, field.attname))

                self.save_base(cls=parent, origin=org, using=using)

                if field:
                    setattr(self, field.attname, self._get_pk_val(parent._meta))
            if meta.proxy:
                return

        if not meta.proxy:
            pk_val = self._get_pk_val(meta)
            pk_is_set = pk_val is not None

            get_args = {'format': ROA_FORMAT}
            get_args.update(ROA_CUSTOM_ARGS)

            # Construct Json payload
            serializer = self.get_serializer(self)
            payload = self.get_renderer().render(serializer.data)

            # Add serializer content_type
            headers = get_roa_headers()
            headers.update(self.get_serializer_content_type())

            # check if resource use custom primary key
            if not meta.pk.attname in ['pk', 'id']:
                # consider it might be inserting so check it first
                # @todo: try to improve this block to check if custom pripary key is not None first
                resource = Resource(self.get_resource_url_detail(),
                                    headers=headers,
                                    filters=ROA_FILTERS)
                try:
                    response = resource.get(payload=None, **get_args)
                except ResourceNotFound:
                    # since such resource does not exist, it's actually creating
                    pk_is_set = False
                except RequestFailed:
                    pk_is_set = False

            if force_update or pk_is_set and not self.pk is None:
                record_exists = True
                resource = Resource(self.get_resource_url_detail(),
                                    filters=ROA_FILTERS)
                try:
                    logger.debug(u"""Modifying : "%s" through %s with payload "%s" and GET args "%s" """ % (
                                  force_unicode(self),
                                  force_unicode(resource.uri),
                                  force_unicode(payload),
                                  force_unicode(get_args)))
                    response = resource.put(payload=payload, headers=headers, **get_args)
                except RequestFailed as e:
                    raise ROAException(e)
            else:
                record_exists = False
                resource = Resource(self.get_resource_url_list(),
                                    filters=ROA_FILTERS)
                try:
                    logger.debug(u"""Creating  : "%s" through %s with payload "%s" and GET args "%s" """ % (
                                  force_unicode(self),
                                  force_unicode(resource.uri),
                                  force_unicode(payload),
                                  force_unicode(get_args)))
                    response = resource.post(payload=payload, headers=headers, **get_args)
                except RequestFailed as e:
                    raise ROAException(e)

            response = force_unicode(response.body_string()).encode(DEFAULT_CHARSET)

            data = self.get_parser().parse(StringIO(response))
            serializer = self.get_serializer(data=data)
            if not serializer.is_valid():
                raise ROAException(u'Invalid deserialization for {} model: {}'.format(self, serializer.errors))
            try:
                self.pk = int(serializer.object.pk)
            except ValueError:
                self.pk = serializer.object.pk
            self = serializer.object

        if origin:
            signals.post_save.send(sender=origin, instance=self,
                created=(not record_exists), raw=raw)
Ejemplo n.º 31
0
class StoreClient(object):
    def __init__(self, endpoint, name, **kwargs):
        if endpoint.endswith('/'):
            endpoint = endpoint.rstrip('/')
        if 'pool' not in kwargs:
            kwargs['pool'] = ConnectionPool(factory=Connection)
        self.json_default = kwargs.get('json_default', json_util.default)
        self.json_object_hook = kwargs.get('json_object_hook',
                                           json_util.object_hook)
        self.resource = Resource(endpoint, **kwargs)
        self.name = name

    def create_store(self, store):
        response = self.resource.post("/stores/%s" % store)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def get_stores(self):
        response = self.resource.get("/stores")
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def create_branch(self, store, branch, parent=None):
        path = _build_path(store, "branch", branch)
        params = _build_params(parent=parent)
        response = self.resource.post(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def get_branch(self, store, branch):
        path = _build_path(store, "branch", branch)
        response = self.resource.get(path)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def merge(self,
              store,
              source,
              target='master',
              author=None,
              committer=None):
        path = _build_path(store, "merge", source)
        params = _build_params(target=target,
                               author=author,
                               committer=committer)
        response = self.resource.post(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def get(self,
            store,
            key=None,
            shallow=False,
            branch='master',
            commit_sha=None):
        path = _entry_path(store, key)
        params = _build_params(shallow=shallow,
                               branch=branch,
                               commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            response_body = response.body_string()
            return json.loads(response_body, object_hook=self.json_object_hook)

    def put(self,
            store,
            key,
            value,
            flatten_keys=True,
            branch='master',
            author=None,
            committer=None):
        path = _entry_path(store, key)
        payload = json.dumps(value, default=self.json_default)
        flatten_keys = 1 if flatten_keys else 0
        params = _build_params(flatten_keys=flatten_keys,
                               branch=branch,
                               author=author,
                               committer=committer)
        headers = {'Content-Type': 'application/json'}
        response = self.resource.put(path,
                                     headers=headers,
                                     payload=payload,
                                     params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def delete(self, store, key, branch='master', author=None, committer=None):
        path = _entry_path(store, key)
        params = _build_params(branch=branch,
                               author=author,
                               committer=committer)
        response = self.resource.delete(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def keys(self,
             store,
             key=None,
             pattern=None,
             depth=None,
             filter_by=None,
             branch='master',
             commit_sha=None):
        path = _build_path(store, "keys", key)
        params = _build_params(pattern=pattern,
                               depth=depth,
                               filter_by=filter_by,
                               branch=branch,
                               commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def entries(self,
                store,
                key=None,
                pattern=None,
                depth=None,
                branch='master',
                commit_sha=None):
        path = _build_path(store, "entries", key)
        params = _build_params(pattern=pattern,
                               depth=depth,
                               branch=branch,
                               commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)

    def trees(self,
              store,
              key=None,
              pattern=None,
              depth=None,
              object_depth=None,
              branch='master',
              commit_sha=None):
        path = _build_path(store, "trees", key)
        params = _build_params(pattern=pattern,
                               depth=depth,
                               object_depth=object_depth,
                               branch=branch,
                               commit_sha=commit_sha)
        response = self.resource.get(path, params_dict=params)
        if response.status_int == 200:
            return json.loads(response.body_string(),
                              object_hook=self.json_object_hook)
Ejemplo n.º 32
0
    def save_base(self, raw=False, cls=None, origin=None, force_insert=False,
            force_update=False, using=None):
        """
        Does the heavy-lifting involved in saving. Subclasses shouldn't need to
        override this method. It's separate from save() in order to hide the
        need for overrides of save() to pass around internal-only parameters
        ('raw', 'cls', and 'origin').
        """
        assert not (force_insert and force_update)
        if cls is None:
            cls = self.__class__
            meta = cls._meta
            if not meta.proxy:
                origin = cls
        else:
            meta = cls._meta

        if origin and not getattr(meta, "auto_created", False):
            signals.pre_save.send(sender=origin, instance=self, raw=raw)

        # If we are in a raw save, save the object exactly as presented.
        # That means that we don't try to be smart about saving attributes
        # that might have come from the parent class - we just save the
        # attributes we have been given to the class we have been given.
        # We also go through this process to defer the save of proxy objects
        # to their actual underlying model.
        if not raw or meta.proxy:
            if meta.proxy:
                org = cls
            else:
                org = None
            for parent, field in meta.parents.items():
                # At this point, parent's primary key field may be unknown
                # (for example, from administration form which doesn't fill
                # this field). If so, fill it.
                if field and getattr(self, parent._meta.pk.attname) is None \
                   and getattr(self, field.attname) is not None:
                    setattr(self, parent._meta.pk.attname, getattr(self, field.attname))

                self.save_base(cls=parent, origin=org)

                if field:
                    setattr(self, field.attname, self._get_pk_val(parent._meta))
            if meta.proxy:
                return

        if not meta.proxy:
            pk_val = self._get_pk_val(meta)
            pk_set = pk_val is not None

            get_args = {'format': ROA_FORMAT}
            get_args.update(getattr(settings, "ROA_CUSTOM_ARGS", {}))

            serializer = serializers.get_serializer(ROA_FORMAT)
            if hasattr(serializer, 'serialize_object'):
                payload = serializer().serialize_object(self)
            else:
                payload = {}
                for field in meta.local_fields:
                    # Handle FK fields
                    if isinstance(field, models.ForeignKey):
                        field_attr = getattr(self, field.name)
                        if field_attr is None:
                            payload[field.attname] = None
                        else:
                            payload[field.attname] = field_attr.pk
                    # Handle all other fields
                    else:
                        payload[field.name] = field.value_to_string(self)

                # Handle M2M relations in case of update
                if force_update or pk_set and not self.pk is None:
                    for field in meta.many_to_many:
                        # First try to get ids from var set in query's add/remove/clear
                        if hasattr(self, '%s_updated_ids' % field.attname):
                            field_pks = getattr(self, '%s_updated_ids' % field.attname)
                        else:
                            field_pks = [obj.pk for obj in field.value_from_object(self)]
                        payload[field.attname] = ','.join(smart_unicode(pk) for pk in field_pks)

            if force_update or pk_set and not self.pk is None:
                record_exists = True
                resource = Resource(self.get_resource_url_detail(),
                                    headers=ROA_HEADERS,
                                    filters=ROA_FILTERS)
                try:
                    logger.debug(u"""Modifying : "%s" through %s
                                  with payload "%s" and GET args "%s" """ % (
                                  force_unicode(self),
                                  force_unicode(resource.uri),
                                  force_unicode(payload),
                                  force_unicode(get_args)))
                    response = resource.put(payload=payload, **get_args)
                except RequestFailed, e:
                    raise ROAException(e)
            else:
                record_exists = False
                resource = Resource(self.get_resource_url_list(),
                                    headers=ROA_HEADERS,
                                    filters=ROA_FILTERS)
                try:
                    logger.debug(u"""Creating  : "%s" through %s
                                  with payload "%s" and GET args "%s" """ % (
                                  force_unicode(self),
                                  force_unicode(resource.uri),
                                  force_unicode(payload),
                                  force_unicode(get_args)))
                    response = resource.post(payload=payload, **get_args)
                except RequestFailed, e:
                    raise ROAException(e)
Ejemplo n.º 33
0
 def update_pull_request(self, user, repo, number, data):
     resource = Resource("https://api.github.com/repos/%s/%s/pulls/%s" %
                         (user, repo, number))
     res = resource.post(headers=self.headers, payload=json.dumps(data))
     return json.loads(res.body_string())