Best Python code snippet using tempest_python
endpoints.py
Source:endpoints.py  
1# -*- coding: utf-8 -*-2#3# Copyright (c) 2019 Ryan Murray.4#5# This file is part of Dremio Client6# (see https://github.com/rymurr/dremio_client).7#8# Licensed to the Apache Software Foundation (ASF) under one9# or more contributor license agreements.  See the NOTICE file10# distributed with this work for additional information11# regarding copyright ownership.  The ASF licenses this file12# to you under the Apache License, Version 2.0 (the13# "License"); you may not use this file except in compliance14# with the License.  You may obtain a copy of the License at15#16# http://www.apache.org/licenses/LICENSE-2.017#18# Unless required by applicable law or agreed to in writing,19# software distributed under the License is distributed on an20# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY21# KIND, either express or implied.  See the License for the22# specific language governing permissions and limitations23# under the License.24#25import requests26import json as jsonlib27from requests.exceptions import HTTPError28from six.moves.urllib.parse import quote29from ..error import (30    DremioBadRequestException,31    DremioException,32    DremioNotFoundException,33    DremioPermissionException,34    DremioUnauthorizedException,35)36def _get_headers(token):37    headers = {"Authorization": "_dremio{}".format(token), "content-type": "application/json"}38    return headers39def _get(url, token, details="", ssl_verify=True):40    r = requests.get(url, headers=_get_headers(token), verify=ssl_verify)41    return _check_error(r, details)42def _post(url, token, json=None, details="", ssl_verify=True):43    if isinstance(json, str):44        json = jsonlib.loads(json)45    r = requests.post(url, headers=_get_headers(token), verify=ssl_verify, json=json)46    return _check_error(r, details)47def _delete(url, token, details="", ssl_verify=True):48    r = requests.delete(url, headers=_get_headers(token), verify=ssl_verify)49    return _check_error(r, details)50def _put(url, token, json=None, details="", ssl_verify=True):51    if isinstance(json, str):52        json = jsonlib.loads(json)53    r = requests.put(url, headers=_get_headers(token), verify=ssl_verify, json=json)54    return _check_error(r, details)55def _patch(url, token, json=None, details="", ssl_verify=True):56    if isinstance(json, str):57        json = jsonlib.loads(json)58    r = requests.patch(url, headers=_get_headers(token) ,verify=ssl_verify, json=json)59    return _check_error(r, details)60def _check_error(r, details=""):61    error, code, _ = _raise_for_status(r)62    if not error:63        try:64            data = r.json()65            return data66        except:  # NOQA67            return r.text68    if code == 400:69        raise DremioBadRequestException("Requested object does not exist on entity " + details, error, r)70    if code == 401:71        raise DremioUnauthorizedException("Unauthorized on api endpoint " + details, error, r)72    if code == 403:73        raise DremioPermissionException("Not permissioned to view entity at " + details, error, r)74    if code == 404:75        raise DremioNotFoundException("No entity exists at " + details, error, r)76    raise DremioException("unknown error", error)77def catalog_item(token, base_url, cid=None, path=None, ssl_verify=True):78    """fetch a specific catalog item by id or by path79    https://docs.dremio.com/rest-api/catalog/get-catalog-id.html80    https://docs.dremio.com/rest-api/catalog/get-catalog-path.html81    :param token: auth token from previous login attempt82    :param base_url: base Dremio url83    :param cid: unique dremio id for resource84    :param path: list ['space', 'folder', 'vds']85    :param ssl_verify: ignore ssl errors if False86    :return: json of resource87    """88    if cid is None and path is None:89        raise TypeError("both id and path can't be None for a catalog_item call")90    idpath = (cid if cid else "") + ", " + (".".join(path) if path else "")91    cpath = [quote(i, safe="") for i in path] if path else ""92    endpoint = "/{}".format(cid) if cid else "/by-path/{}".format("/".join(cpath).replace('"', ""))93    return _get(base_url + "/api/v3/catalog{}".format(endpoint), token, idpath, ssl_verify=ssl_verify)94def catalog(token, base_url, ssl_verify=True):95    """96    https://docs.dremio.com/rest-api/catalog/get-catalog.html populate the root dremio catalog97    :param token: auth token from previous login attempt98    :param base_url: base Dremio url99    :param ssl_verify: ignore ssl errors if False100    :return: json of root resource101    """102    return _get(base_url + "/api/v3/catalog", token, ssl_verify=ssl_verify)103def sql(token, base_url, query, context=None, ssl_verify=True):104    """submit job w/ given sql105    https://docs.dremio.com/rest-api/sql/post-sql.html106    :param token: auth token107    :param base_url: base Dremio url108    :param query: sql query109    :param context: optional dremio context110    :param ssl_verify: ignore ssl errors if False111    :return: job id json object112    """113    return _post(base_url + "/api/v3/sql", token, ssl_verify=ssl_verify, json={"sql": query, "context": context})114def job_status(token, base_url, job_id, ssl_verify=True):115    """fetch job status116    https://docs.dremio.com/rest-api/jobs/get-job.html117    :param token: auth token118    :param base_url: sql query119    :param job_id: job id (as returned by sql)120    :param ssl_verify: ignore ssl errors if False121    :return: status object122    """123    return _get(base_url + "/api/v3/job/{}".format(job_id), token, ssl_verify=ssl_verify)124def job_results(token, base_url, job_id, offset=0, limit=100, ssl_verify=True):125    """fetch job results126    https://docs.dremio.com/rest-api/jobs/get-job.html127    :param token: auth token128    :param base_url: sql query129    :param job_id: job id (as returned by sql)130    :param offset: offset of result set to return131    :param limit: number of results to return (max 500)132    :param ssl_verify: ignore ssl errors if False133    :return: result object134    """135    return _get(136        base_url + "/api/v3/job/{}/results?offset={}&limit={}".format(job_id, offset, limit),137        token,138        ssl_verify=ssl_verify,139    )140def reflections(token, base_url, summary=False, ssl_verify=True):141    """fetch all reflections142    https://docs.dremio.com/rest-api/reflections/get-reflection.html143    :param token: auth token144    :param base_url: sql query145    :param summary: fetch only the reflection summary146    :param ssl_verify: ignore ssl errors if False147    :return: result object148    """149    return _get(base_url + "/api/v3/reflection" + ("/summary" if summary else ""), token, ssl_verify=ssl_verify)150def reflection(token, base_url, reflectionid, ssl_verify=True):151    """fetch a single reflection by id152    https://docs.dremio.com/rest-api/reflections/get-reflection.html153    :param token: auth token154    :param base_url: sql query155    :param reflectionid: id of the reflection to fetch156    :param ssl_verify: ignore ssl errors if False157    :return: result object158    """159    return _get(base_url + "/api/v3/reflection/{}".format(reflectionid), token, ssl_verify=ssl_verify)160def wlm_queues(token, base_url, ssl_verify=True):161    """fetch all wlm queues162    https://docs.dremio.com/rest-api/wlm/get-wlm-queue.html163    :param token: auth token164    :param base_url: sql query165    :param ssl_verify: ignore ssl errors if False166    :return: result object167    """168    return _get(base_url + "/api/v3/wlm/queue", token, ssl_verify=ssl_verify)169def wlm_queue(token, base_url, qid=None, name=None, ssl_verify=True):170    """fetch wlm queue by id or name171    https://docs.dremio.com/rest-api/wlm/get-wlm-queue.html172    :param token: auth token173    :param base_url: sql query174    :param qid: unique queue id175    :param name: name for a queue176    :ssl_verify: ignore ssl errors if False177    :return: result object178    """179    if qid is None and name is None:180        raise TypeError("both id and name cannot be None for a GET queue call")181    if qid is not None:182        endurl = base_url + "/api/v3/wlm/queue/{}".format(qid)183        return _get(endurl, token, endurl, ssl_verify)184    else:185        endurl = base_url + "/api/v3/wlm/queue/by-name/{}".format(name)186        return _get(endurl, token, endurl, ssl_verify)187def wlm_rules(token, base_url, ssl_verify=True):188    """fetch all wlm rules189    https://docs.dremio.com/rest-api/wlm/get-wlm-rule.html190    :param token: auth token191    :param base_url: sql query192    :param ssl_verify: ignore ssl errors if False193    :return: result object194    """195    return _get(base_url + "/api/v3/wlm/rule", token, ssl_verify=ssl_verify)196def votes(token, base_url, ssl_verify=True):197    """fetch all votes198    https://docs.dremio.com/rest-api/reflections/get-vote.html199    :param token: auth token200    :param base_url: sql query201    :param ssl_verify: ignore ssl errors if False202    :return: result object203    """204    return _get(base_url + "/api/v3/vote", token, ssl_verify=ssl_verify)205def create_user(token, base_url, json, ssl_verify=True):206    """207    :param token: auth token208    :param base_url: sql query209    :param json: json document for creating new user210    :param ssl_verify: ignore ssl errors if False211    :return: result object212    """213    return _post(base_url + "/api/v3/user", token, json, ssl_verify=ssl_verify)214def delete_user(token, base_url, uid ,tag , ssl_verify=True):215    """216    Deletes the given user if it exists217    :param token: auth token218    :param base_url: sql query219    :param uid: user id220    :param tag: version parameter of user221    :param ssl_verify: ignore ssl errors if False222    :return: None223    """224    parsed_tag = quote(tag, safe="")225    return _delete(base_url + "/api/v3/user/{}?version={}".format(uid,parsed_tag) , token, ssl_verify=ssl_verify)226def update_user(token, base_url, uid, json, ssl_verify=True):227    """228    Returns the user info after updating it229    :param token: auth token230    :param base_url: sql query231    :param uid: user id232    :param json: json document for role233    :param ssl_verify: ignore ssl errors if False234    :return: result object235    """236    return _put(base_url + "/api/v3/user/{}".format(uid), token, json, ssl_verify=ssl_verify)237def get_privileges_of_user(token,base_url ,uid ,startIndex=None, count=None ,ssl_verify=True):238    """239    Fetches all the privileges of a user240    :param token: auth token241    :param base_url: sql query242    :param uid: user id243    :param startIndex: index starting from which to fetch privileges244    :param count: maximum number of privileges to fetch245    :param ssl_verify: ignore ssl errors if False246    :return: result object247    """248    end_url = base_url + "/api/v3/user/{}/privilege".format(uid) + build_url(startIndex=startIndex,count=count)249    return _get(end_url, token, ssl_verify=ssl_verify)250def user(token, base_url, uid=None, name=None, ssl_verify=True):251    """252    fetch user based on id or name253    https://docs.dremio.com/rest-api/reflections/get-user.html254    :param token: auth token255    :param base_url: sql query256    :param uid: unique dremio id for user257    :param name: name for a user258    :param ssl_verify: ignore ssl errors if False259    :return: result object260    """261    if uid is None and name is None:262        raise TypeError("both id and name can't be None for a user call")263    if uid is not None:264        endurl = base_url + "/api/v3/user/{}".format(uid)265        return _get(endurl, token, endurl, ssl_verify)266    else:267        endurl = base_url + "/api/v3/user/by-name/{}".format(name)268        return _get(endurl, token, endurl, ssl_verify)269def create_role(token,base_url,json ,ssl_verify=True):270    """271    :param token: auth token272    :param base_url: sql query273    :param json: json document for creating new role274    :param ssl_verify: ignore ssl errors if False275    :return: result object276    """277    return _post(base_url + "/api/v3/role", token, json, ssl_verify=ssl_verify)278def get_role(token, base_url, rid, name, ssl_verify=True):279    """280    Returns role info for a role with given id or name281    :param token: auth token282    :param base_url: sql query283    :param rid: role id284    :param name: role name285    :param ssl_verify: ignore ssl errors if False286    :return: result object287    """288    if rid is None and name is None:289        raise TypeError("both id and name can't be None for a user call")290    if rid is not None :291        return _get(base_url + "/api/v3/role/{}".format(rid), token, ssl_verify=ssl_verify)292    else:293        return _get(base_url + "/api/v3/role/by-name/{}".format(name), token, ssl_verify=ssl_verify)294def delete_role(token, base_url, rid , ssl_verify=True):295    """296    Deletes the role with a given rid297    :param token: auth token298    :param base_url: sql query299    :param rid: role id300    :param ssl_verify: ignore ssl errors if False301    :return: None302    """303    return _delete(base_url + "/api/v3/role/{}".format(rid) , token, ssl_verify=ssl_verify)304def update_role(token, base_url, rid, json, ssl_verify=True):305    """306    Returns the role after updating it307    :param token: auth token308    :param base_url: sql query309    :param rid: role id310    :param json: json document for role311    :param ssl_verify: ignore ssl errors if False312    :return: result object313    """314    return _put(base_url + "/api/v3/role/{}".format(rid), token, json, ssl_verify=ssl_verify)315def get_privileges_of_role(token, base_url, rid, startIndex=None, count=None, ssl_verify=True):316    """317    Fetches privileges of a given role318    :param token: auth token319    :param base_url: sql query320    :param rid: role id321    :param startIndex: index starting from which to fetch privileges322    :param count: maximum number of privileges to fetch323    :param ssl_verify: ignore ssl errors if False324    :return: result object325    """326    end_url = base_url + "/api/v3/role/{}/privilege".format(rid) + build_url(startIndex=startIndex , count=count)327    return _get(end_url, token, ssl_verify=ssl_verify)328def update_member_of_role(token, base_url, rid, json, ssl_verify=True):329    """330    Add remove a member from a role331    :param token: auth token332    :param base_url: sql query333    :param rid: role id334    :param json: json document of role335    :param ssl_verify: Ignore ssl errors if False336    :return: result object337    """338    return _patch(base_url + "/api/v3/role/{}/member".format(rid), token, json, ssl_verify=ssl_verify)339def group(token, base_url, gid=None, name=None, ssl_verify=True):340    """fetch a group based on id or name341    https://docs.dremio.com/rest-api/reflections/get-group.html342    :param token: auth token343    :param base_url: sql query344    :param gid: unique dremio id for group345    :param name: name for a group346    :param ssl_verify: ignore ssl errors if False347    :return: result object348    """349    if gid is None and name is None:350        raise TypeError("both id and name can't be None for a group call")351    if gid is not None:352        endurl = base_url + "/api/v3/group/{}".format(gid)353        return _get(endurl, token, endurl, ssl_verify)354    else:355        endurl = base_url + "/api/v3/group/by-name/{}".format(name)356        return _get(endurl, token, endurl, ssl_verify)357def personal_access_token(token, base_url, uid, ssl_verify=True):358    """fetch a PAT for a user based on id359    https://docs.dremio.com/rest-api/user/get-user-id-token.html360    :param token: auth token361    :param base_url: sql query362    :param uid: id of a user363    :return: result object364    :param ssl_verify: ignore ssl errors if False365    """366    return _get(base_url + "/api/v3/user/{}/token".format(uid), token, ssl_verify=ssl_verify)367def collaboration_tags(token, base_url, cid, ssl_verify=True):368    """fetch tags for a catalog entry369    https://docs.dremio.com/rest-api/user/get-catalog-collaboration.html370    :param token: auth token371    :param base_url: sql query372    :param cid: id of a catalog entity373    :param ssl_verify: ignore ssl errors if False374    :return: result object375    """376    return _get(base_url + "/api/v3/catalog/{}/collaboration/tag".format(cid), token, ssl_verify=ssl_verify)377def collaboration_wiki(token, base_url, cid, ssl_verify=True):378    """fetch wiki for a catalog entry379    https://docs.dremio.com/rest-api/user/get-catalog-collaboration.html380    :param token: auth token381    :param base_url: sql query382    :param cid: id of a catalog entity383    :param ssl_verify: ignore ssl errors if False384    :return: result object385    """386    return _get(base_url + "/api/v3/catalog/{}/collaboration/wiki".format(cid), token, ssl_verify=ssl_verify)387def refresh_pds(token, base_url, pid, ssl_verify=True):388    """ refresh a physical dataset and all its child reflections389    https://docs.dremio.com/rest-api/catalog/post-catalog-id-refresh.html390    :param token: auth token391    :param base_url: sql query392    :param pid: id of a catalog entity393    :param ssl_verify: ignore ssl errors if False394    :return: None395    """396    return _post(base_url + "/api/v3/catalog/{}/refresh".format(pid), token, ssl_verify=ssl_verify)397def set_collaboration_tags(token, base_url, cid, tags, ssl_verify=True):398    """ set tags on a given catalog entity399    https://docs.dremio.com/rest-api/catalog/post-catalog-collaboration.html400    :param token: auth token401    :param base_url: sql query402    :param cid: id of a catalog entity403    :param tags: list of strings for tags404    :param ssl_verify: ignore ssl errors if False405    :return: None406    """407    json = {"tags": tags}408    try:409        old_tags = collaboration_tags(token, base_url, cid, ssl_verify)410        json["version"] = old_tags["version"]411    except:  # NOQA412        pass413    return _post(base_url + "/api/v3/catalog/{}/collaboration/tag".format(cid), token, ssl_verify=ssl_verify, json=json)414def set_collaboration_wiki(token, base_url, cid, wiki, ssl_verify=True):415    """ set wiki on a given catalog entity416    https://docs.dremio.com/rest-api/catalog/post-catalog-collaboration.html417    :param token: auth token418    :param base_url: sql query419    :param cid: id of a catalog entity420    :param wiki: text representing markdown for entity421    :param ssl_verify: ignore ssl errors if False422    :return: None423    """424    json = {"text": wiki}425    try:426        old_wiki = collaboration_wiki(token, base_url, cid, ssl_verify)427        json["version"] = old_wiki["version"]428    except:  # NOQA429        pass430    return _post(431        base_url + "/api/v3/catalog/{}/collaboration/wiki".format(cid), token, ssl_verify=ssl_verify, json=json432    )433def delete_catalog(token, base_url, cid, tag, ssl_verify=True):434    """ remove a catalog item from Dremio435    https://docs.dremio.com/rest-api/catalog/delete-catalog-id.html436    :param token: auth token437    :param base_url: sql query438    :param cid: id of a catalog entity439    :param tag: version tag of entity440    :param ssl_verify: ignore ssl errors if False441    :return: None442    """443    if tag is None:444        return _delete(base_url + "/api/v3/catalog/{}".format(cid), token, ssl_verify=ssl_verify)445    else:446        return _delete(base_url + "/api/v3/catalog/{}?tag={}".format(cid, tag), token, ssl_verify=ssl_verify)447def set_catalog(token, base_url, json, ssl_verify=True):448    """ add a new catalog entity449    https://docs.dremio.com/rest-api/catalog/post-catalog.html450    :param token: auth token451    :param base_url: sql query452    :param json: json document for new catalog entity453    :param ssl_verify: ignore ssl errors if False454    :return: new catalog entity455    """456    return _post(base_url + "/api/v3/catalog", token, json, ssl_verify=ssl_verify)457def update_catalog(token, base_url, cid, json, ssl_verify=True):458    """ update a catalog entity459    https://docs.dremio.com/rest-api/catalog/put-catalog-id.html460    :param token: auth token461    :param base_url: sql query462    :param cid: id of catalog entity463    :param json: json document for new catalog entity464    :param ssl_verify: ignore ssl errors if False465    :return: updated catalog entity466    """467    return _put(base_url + "/api/v3/catalog/{}".format(cid), token, json, ssl_verify=ssl_verify)468def promote_catalog(token, base_url, cid, json, ssl_verify=True):469    """ promote a catalog entity (only works on folders and files in sources470    https://docs.dremio.com/rest-api/catalog/post-catalog-id.html471    :param token: auth token472    :param base_url: sql query473    :param cid: id of catalog entity474    :param json: json document for new catalog entity475    :param ssl_verify: ignore ssl errors if False476    :return: updated catalog entity477    """478    return _post(base_url + "/api/v3/catalog/{}".format(cid), token, json, ssl_verify=ssl_verify)479def set_personal_access_token(token, base_url, uid, label, lifetime=24, ssl_verify=True):480    """ create a pat for a given user481    https://docs.dremio.com/rest-api/user/post-user-uid-token.html482    :param token: auth token483    :param base_url: sql query484    :param uid: id user485    :param label: label of token486    :param lifetime: lifetime in hours of token487    :param ssl_verify: ignore ssl errors if False488    :return: updated catalog entity489    """490    return _post(491        base_url + "/api/v3/user/{}/token".format(uid),492        token,493        {"label": label, "millisecondsToExpire": 1000 * 60 * 60 * lifetime},494        ssl_verify=ssl_verify,495    )496def delete_personal_access_token(token, base_url, uid=None, tid=None, ssl_verify=True):497    """ delete a personal access token.498    https://docs.dremio.com/rest-api/user/delete-user-uid-token.html499    https://docs.dremio.com/rest-api/token/500    :param token: auth token501    :param base_url: sql query502    :param uid: id user (optional)503    :param tid: label of token (optional)504    :param ssl_verify: ignore ssl errors if False505    :return: updated catalog entity506    """507    url_user_component = "user/{}/".format(uid) if uid else ""508    url = base_url + "/api/v3/{}token{}".format(url_user_component, ("/" + tid) if tid else "")509    return _delete(url, token, ssl_verify=ssl_verify)510def modify_reflection(token, base_url, reflectionid, json, ssl_verify=True):511    """update a single reflection by id512    https://docs.dremio.com/rest-api/reflections/put-reflection.html513    :param token: auth token514    :param base_url: sql query515    :param reflectionid: id of the reflection to fetch516    :param json: json document for modified reflection517    :param ssl_verify: ignore ssl errors if False518    :return: result object519    """520    return _put(base_url + "/api/v3/reflection/{}".format(reflectionid), token, json, ssl_verify=ssl_verify)521def create_reflection(token, base_url, json, ssl_verify=True):522    """create a single reflection523    https://docs.dremio.com/rest-api/reflections/post-reflection.html524    :param token: auth token525    :param base_url: sql query526    :param json: json document for new reflection527    :param ssl_verify: ignore ssl errors if False528    :return: result object529    """530    return _post(base_url + "/api/v3/reflection/", token, json, ssl_verify=ssl_verify)531def delete_reflection(token, base_url, reflectionid, ssl_verify=True):532    """delete a single reflection by id533    https://docs.dremio.com/rest-api/reflections/delete-reflection.html534    :param token: auth token535    :param base_url: sql query536    :param reflectionid: id of the reflection to delete537    :param ssl_verify: ignore ssl errors if False538    :return: result object539    """540    _delete(base_url + "/api/v3/reflection/{}".format(reflectionid), token, ssl_verify=ssl_verify)541def cancel_job(token, base_url, jid, ssl_verify=True):542    """cancel running job with job id = jid543    https://docs.dremio.com/rest-api/jobs/post-job.html544    :param token: auth token545    :param base_url: sql query546    :param jid: id of the job to cancel547    :param ssl_verify: ignore ssl errors if False548    :return: result object549    :exception DremioNotFoundException no job found550    :exception DremioBadRequestException job already finished551    """552    _post(base_url + "/api/v3/job/{}/cancel".format(jid), token, ssl_verify=ssl_verify)553def modify_queue(token, base_url, queueid, json, ssl_verify=True):554    """update a single queue by id555    https://docs.dremio.com/rest-api/wlm/put-wlm-queue.html556    :param token: auth token557    :param base_url: sql query558    :param queueid: id of the reflection to fetch559    :param json: json document for modified queue560    :param ssl_verify: ignore ssl errors if False561    :return: result object562    """563    return _put(base_url + "/api/v3/wlm/queue/{}".format(queueid), token, json, ssl_verify=ssl_verify)564def create_queue(token, base_url, json, ssl_verify=True):565    """create a single queue566    https://docs.dremio.com/rest-api/wlm/post-wlm-queue.html567    :param token: auth token568    :param base_url: sql query569    :param json: json document for new queue570    :param ssl_verify: ignore ssl errors if False571    :return: result object572    """573    return _post(base_url + "/api/v3/wlm/queue/", token, json, ssl_verify=ssl_verify)574def delete_queue(token, base_url, queueid, ssl_verify=True):575    """delete a single queue by id576    https://docs.dremio.com/rest-api/wlm/delete-wlm-queue.html577    :param token: auth token578    :param base_url: sql query579    :param queueid: id of the queue to delete580    :param ssl_verify: ignore ssl errors if False581    :return: result object582    """583    _delete(base_url + "/api/v3/wlm/queue/{}".format(queueid), token, ssl_verify=ssl_verify)584def modify_rules(token, base_url, json, ssl_verify=True):585    """update wlm rules. Order of rules array is important!586    The order of the rules is the order in which they will be applied. If a rule isn't included it will be deleted587    new ones will be created588    https://docs.dremio.com/rest-api/wlm/put-wlm-rule.html589    :param token: auth token590    :param base_url: sql query591    :param json: json document for modified reflection592    :param ssl_verify: ignore ssl errors if False593    :return: result object594    """595    return _put(base_url + "/api/v3/wlm/rule/", token, json, ssl_verify=ssl_verify)596def get_privilege(token, base_url, pid=None, ssl_verify=True):597    if pid is None:598        raise TypeError("resource id can't be None for a privilege call")599    return _get(base_url + "/api/v3/catalog/{}/grants".format(pid), token, ssl_verify=ssl_verify)600def get_privilege_by_grant_type(token, base_url, grantType="", ssl_verify=True):601    if grantType == "":602        raise TypeError("resource grantType can't be empty for a privilege call")603    return _get(base_url + "/api/v3/catalog/privileges?type={}".format(grantType), token, ssl_verify=ssl_verify)604def get_privileges_by_grant(token , base_url ,grantType=None ,ssl_verify=True):605    """606    Gets all available privileges for a grant (This api isn't implemented as of now)607    :param token: auth token608    :param base_url: sql query609    :param grantType: optional parameter type of grant (example PROJECT)610    :param ssl_verify: Ignore  ssl errors if False611    :return: result object612    """613    if grantType:614        end_url=base_url + "/api/v3/grant?grantType={}".format(grantType)615    else :616        end_url =base_url + "/api/v3/grant"617    return _get(end_url ,token,ssl_verify=ssl_verify)618def get_grants_of_grantee(token, base_url ,granteeType ,granteeId,grantType=None , ssl_verify=True):619    """620    Gets all grants of a specific grantee621    :param token: auth token622    :param base_url: sql query623    :param granteeType: type of grantee (user or role)624    :param granteeId: id of grantee625    :param grantType: type of grant (example PROJECT)626    :param ssl_verify: ignore ssl errors if False627    :return: result object628    """629    if grantType:630        end_url=base_url + "/api/v3/grant/{}/{}?grantType={}".format(granteeType,granteeId,grantType)631    else :632        end_url=base_url + "/api/v3/grant/{}/{}".format(granteeType,granteeId)633    return _get(end_url, token,ssl_verify=ssl_verify)634def update_grants_of_grantee(token, base_url, granteeId , grantee ,json,ssl_verify=True):635    """636    Updates grants of a particular grantee637    :param token: auth token638    :param base_url: sql query639    :param granteeId:id of grantee (should match with one in json)640    :param grantee :user or role641    :param json: json document for updation (id must match with one in url)642    :param ssl_verify: Ignore ssl errors if False643    :return: result object644    """645    return _put(base_url+ "/api/v3/grant/{}/{}".format(grantee,granteeId) , token, json , ssl_verify=ssl_verify)646def update_privilege(token, base_url, pid, json, ssl_verify=True):647    return _put(base_url + "/api/v3/catalog/{}/grants".format(pid), token, json, ssl_verify=ssl_verify)648def delete_privilege(token, base_url, pid, grants, ssl_verify=True):649    return _delete(base_url + "/api/v3/catalog/{}/{}".format(pid, grants), token, ssl_verify=ssl_verify)650def _raise_for_status(self):651    """Raises stored :class:`HTTPError`, if one occurred. Copy from requests request.raise_for_status()"""652    http_error_msg = ""653    if isinstance(self.reason, bytes):654        try:655            reason = self.reason.decode("utf-8")656        except UnicodeDecodeError:657            reason = self.reason.decode("iso-8859-1")658    else:659        reason = self.reason660    if 400 <= self.status_code < 500:661        http_error_msg = u"%s Client Error: %s for url: %s" % (self.status_code, reason, self.url)662    elif 500 <= self.status_code < 600:663        http_error_msg = u"%s Server Error: %s for url: %s" % (self.status_code, reason, self.url)664    if http_error_msg:665        return HTTPError(http_error_msg, response=self), self.status_code, reason666    else:667        return None, self.status_code, reason668def graph(token, base_url, cid=None, ssl_verify=True):669    """Retrieves graph information about a specific catalog entity by id670    https://docs.dremio.com/rest-api/catalog/get-catalog-id-graph.html671    :param token: auth token from previous login attempt672    :param base_url: base Dremio url673    :param cid: unique dremio id for resource674    :param ssl_verify: ignore ssl errors if False675    :return: json of resource676    """677    if cid is None:678        raise TypeError("resource id can't be None for a graph call")679    return _get(base_url + "/api/v3/catalog/{}/graph".format(cid), token, ssl_verify=ssl_verify)680def build_url(**kwargs):681    """682    returns required url string683    :param kwargs: keyword arguments (dictionary)684    :return:string685    """686    query = "&".join("{}={}".format(k,v) for k,v in kwargs.items() if v)687    if query:688        qry= "?{}".format(query)689        return qry...constants.py
Source:constants.py  
1# -*- coding: cp1252 -*-2'''3Created on 31 ago. 20194@author: joseb5API de AEMET: https://opendata.aemet.es/centrodedescargas/AEMETApi6'''7import os8import pathlib9#from pathlib import Path10# Constants11FILE_DIR = os.path.dirname(os.path.abspath(__file__)) #Equivale a FILE_DIR = pathlib.Path(__file__).parent12WORK_DIR = os.path.abspath(os.path.join(FILE_DIR, '..'))13HOME_DIR = str(pathlib.Path.home())14# RAIZ_DIR = os.path.abspath(os.path.join(FILE_DIR, '..'))15# HOME_DIR = str(Path.home())16# FILE_DIR = os.path.dirname(os.path.abspath(__file__)) #Equivale a FILE_DIR = pathlib.Path(__file__).parent17# RAIZ_DIR = os.path.abspath(os.path.join(FILE_DIR, '..'))18# BASE_DIR = os.path.abspath('.')19# PADRE_DIR = os.path.abspath('..') #Equivale a PADRE_DIR = os.path.abspath(r'../')20# WORK_DIR = RAIZ_DIR21AEMET_DIR1 = os.path.join(HOME_DIR, '.aemet')22AEMET_DIR2 = os.path.join(FILE_DIR, '.aemet')23if not os.path.exists(AEMET_DIR1) and not os.path.exists(AEMET_DIR2):24    # Create ~/.aemet config dir25    os.mkdir(os.path.join(HOME_DIR, '.aemet'))26#Obtención del API_KEY en:27#  https://opendata.aemet.es/centrodedescargas/altaUsuario28API_KEY_FILE1 = os.path.join(HOME_DIR, '.aemet', 'api.key')29API_KEY_FILE2 = os.path.join(FILE_DIR, '.aemet', 'api.key')30if os.path.exists(API_KEY_FILE1):31    try:32        API_KEY = open(API_KEY_FILE1, 'r').read().strip()33        API_KEY_FILE = API_KEY_FILE134    except:35        API_KEY = ''36elif os.path.exists(API_KEY_FILE2):37    try:38        API_KEY = open(API_KEY_FILE2, 'r').read().strip()39        API_KEY_FILE = API_KEY_FILE240    except:41        API_KEY = ''42else:43    API_KEY = ''44    API_KEY_FILE = ''45    #REMOVE: valorar si quito este mensaje que aparece al importar tormetron:46    print('Dev', end=' --> ')47    print('Warning:')48    print(f'\tFichero api.key no encontrado en {AEMET_DIR1} ni {AEMET_DIR2}')49    print('\tSi se descargan datos de la API de AEMET se solicitará entrada manual de API_KEY')50    print('\t\tPara obtener una APY_KEY ir a: https://opendata.aemet.es/centrodedescargas/altaUsuario')51    print('\tPara descargar imagenes de precipitación acumulada de 6 horas no es necesaria la API_KEY')52    #REMOVE/>53    54# Endpoints55BASE_URL = 'https://opendata.aemet.es/opendata/api'56MUNICIPIOS_API_URL = BASE_URL + '/maestro/municipios/'57PREDICCION_SEMANAL_API_URL = BASE_URL + '/prediccion/especifica/municipio/diaria/'58PREDICCION_POR_HORAS_API_URL = BASE_URL + '/prediccion/especifica/municipio/horaria/'59PREDICCION_NORMALIZADA_API_URL = BASE_URL + '/prediccion/{}/{}/{}'60PREDICCION_MARITIMA_ALTA_MAR_API_URL = BASE_URL + '/prediccion/maritima/altamar/area/{}'61PREDICCION_MARITIMA_COSTERA_API_URL = BASE_URL + '/prediccion/maritima/costera/costa/{}'62PREDICCION_ESPECIFICA_MONTANYA_API_URL = BASE_URL + '/prediccion/especifica/montaña/pasada/area/{}'63PREDICCION_ESPECIFICA_MONTANYA_DIA_API_URL = BASE_URL + '/prediccion/especifica/montaña/pasada/area/{}/dia/{}'64PREDICCION_ESPECIFICA_PLAYA_API_URL = BASE_URL + '/prediccion/especifica/playa/{}/'65PREDICCION_ESPECIFICA_UVI_API_URL = BASE_URL + '/prediccion/especifica/uvi/{}'66PREDICCION_NIVOLOGICA_API_URL = BASE_URL + '/prediccion/especifica/nivologica/{}'  # 0 / 167ESTACIONES_EMA_API_URL = BASE_URL + '/valores/climatologicos/inventarioestaciones/todasestaciones'68VALORES_CLIMATOLOGICOS_NORMALES = BASE_URL + '/valores/climatologicos/normales/estacion/{}'69VALORES_CLIMATOLOGICOS_EXTREMOS = BASE_URL + '/valores/climatologicos/valoresextremos/parametro/{}/estacion/{}'70VALORES_CLIMATOLOGICOS_MENSUALES = BASE_URL + '/valores/climatologicos/mensualesanuales/datos/anioini/{}/aniofin/{}/estacion/{}'71PRODUCTOS_CLIMATOLOGICOS_API_URL = BASE_URL + '/productos/climatologicos/balancehidrico/{}/{}/'72RESUMEN_CLIMATOLOGICO_MENSUAL_API_URL = BASE_URL + '/productos/climatologicos/resumenclimatologico/nacional/{}/{}/'73OBSERVACION_CONVENCIONAL_API_URL = BASE_URL + '/observacion/convencional/todas/'74OBSERVACION_CONVENCIONAL_ESTACION_API_URL = BASE_URL + '/observacion/convencional/datos/estacion/{}/'75MAPA_RIESGO_INCENDIOS_ESTIMADO = BASE_URL + '/incendios/mapasriesgo/estimado/area/{}'76MAPA_RIESGO_INCENDIOS_PREVISTO = BASE_URL + '/incendios/mapasriesgo/previsto/dia/{}/area/{}'77MAPA_ANALISIS_API_URL = BASE_URL + '/mapasygraficos/analisis/'78MAPAS_SIGNIFICATIVOS_FECHA_API_URL = BASE_URL + '/mapasygraficos/mapassignificativos/fecha/{}/{}/{}/'79MAPAS_SIGNIFICATIVOS_API_URL = BASE_URL + '/mapasygraficos/mapassignificativos/{}/{}/'80MAPA_RAYOS_API_URL = BASE_URL + '/red/rayos/mapa/'81RADAR_NACIONAL_API_URL = BASE_URL + '/red/radar/nacional'82RADAR_REGIONAL_API_URL = BASE_URL + '/red/radar/regional/vd'83SATELITE_SST = BASE_URL + '/satelites/producto/sst/'84SATELITE_NVDI = BASE_URL + '/satelites/producto/nvdi/'85CONTAMINACION_FONDO_ESTACION_API_URL = BASE_URL + '/red/especial/contaminacionfondo/estacion/{}/'86# Params87MAPAS_SIGNIFICATIVOS_DIAS = {88    'HOY_0_12': 'a',89    'HOY_12_24': 'b',90    'MANANA_0_12': 'c',91    'MANANA_12_24': 'd',92    'PASADO_MANANA_0_12': 'e',93    'PASADO_MANANA_12_24': 'f'94}95VCP, VCT, VCV = 'P', 'T', 'V'96TIPO_COSTERA, TIPO_ALTA_MAR = 'costera', 'altamar'97PERIODO_SEMANA, PERIODO_DIA = 'PERIODO_SEMANA', 'PERIODO_DIA'98INCENDIOS_MANANA, INCENDIOS_PASADO_MANANA, INCENDIOS_EN_3_DIAS = range(1, 4)99PENINSULA, CANARIAS, BALEARES = 'p', 'c', 'b'100NACIONAL, CCAA, PROVINCIA = 'nacional', 'ccaa', 'provincia'...source.py
Source:source.py  
1#2# Copyright (c) 2021 Airbyte, Inc., all rights reserved.3#4from typing import Any, List, Mapping, Tuple5import pendulum6import requests7from airbyte_cdk.logger import AirbyteLogger8from airbyte_cdk.models import SyncMode9from airbyte_cdk.sources import AbstractSource10from airbyte_cdk.sources.streams import Stream11from airbyte_cdk.sources.streams.http.auth import TokenAuthenticator12from .streams import (13    Annotations,14    Cohorts,15    Events,16    EventsSessions,17    FeatureFlags,18    Insights,19    InsightsPath,20    InsightsSessions,21    Persons,22    PingMe,23    Trends,24)25DEFAULT_BASE_URL = "https://app.posthog.com"26class SourcePosthog(AbstractSource):27    def check_connection(self, logger: AirbyteLogger, config: Mapping[str, Any]) -> Tuple[bool, Any]:28        try:29            _ = pendulum.parse(config["start_date"])30            authenticator = TokenAuthenticator(token=config["api_key"])31            base_url = config.get("base_url", DEFAULT_BASE_URL)32            stream = PingMe(authenticator=authenticator, base_url=base_url)33            records = stream.read_records(sync_mode=SyncMode.full_refresh)34            _ = next(records)35            return True, None36        except Exception as e:37            if isinstance(e, requests.exceptions.HTTPError) and e.response.status_code == requests.codes.UNAUTHORIZED:38                return False, f"Please check you api_key. Error: {repr(e)}"39            return False, repr(e)40    def streams(self, config: Mapping[str, Any]) -> List[Stream]:41        """42        event/sessions stream is dynamic. Probably, it contains a list of CURRENT sessions.43        In Next day session may expire and wont be available via this endpoint.44        So we need a dynamic load data before tests.45        This stream was requested to be removed due to this reason.46        """47        authenticator = TokenAuthenticator(token=config["api_key"])48        base_url = config.get("base_url", DEFAULT_BASE_URL)49        return [50            Annotations(authenticator=authenticator, start_date=config["start_date"], base_url=base_url),51            Cohorts(authenticator=authenticator, base_url=base_url),52            Events(authenticator=authenticator, start_date=config["start_date"], base_url=base_url),53            EventsSessions(authenticator=authenticator, base_url=base_url),54            FeatureFlags(authenticator=authenticator, base_url=base_url),55            Insights(authenticator=authenticator, base_url=base_url),56            InsightsPath(authenticator=authenticator, base_url=base_url),57            InsightsSessions(authenticator=authenticator, base_url=base_url),58            Persons(authenticator=authenticator, base_url=base_url),59            Trends(authenticator=authenticator, base_url=base_url),...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
