def test_import_with_db_exists_ok_if_empty__empty_db_exists(
        setup_masterdb, drop_dbs):
    data = [
        {
            "_id": "testdoc_1",
            "_rev": "3-825cb35de44c433bfb2df415563a19de"
        },
        {
            "_id": "testdoc_2",
            "_rev": "1-c3d84a0ca6114a8e8fbef75dc8c7be00",
            "test": "test",
        },
        {
            "_id": "testdoc_3",
            "_rev": "15-305afde91ffef71edcff06458e17c186",
            "test": "test",
            "another": "test",
        },
        {
            "_id": "testdoc_4",
            "_rev": "1-967a00dff5e02add41819138abb3284d"
        },
    ]
    with NamedTemporaryFile() as tmpfile:
        filename = tmpfile.name
        print(filename)

    with open(filename, "w", encoding="utf8") as inf:
        inf.write("\n".join(json.dumps(x) for x in data))

    with sessions.BaseUrlSession(base_url="http://localhost:5984/") as client:
        client.auth = ("admin", "adminadmin")
        response = client.put("import_testdb_exists")
        assert response.status_code == 201

    result = runner.invoke(
        app,
        [
            "import",
            "--user",
            "admin",
            "--password",
            "adminadmin",
            filename,
            "import_testdb_exists",
        ],
    )

    print(result.stdout)
    assert result.exit_code == 0

    with sessions.BaseUrlSession(
            base_url="http://localhost:5984/import_testdb_exists/",
    ) as client:
        client.auth = ("admin", "adminadmin")
        assert len(data) == client.get("").json()["doc_count"]
        for doc in data:
            response = client.get(doc["_id"])
            assert response.status_code == 200
            assert doc == response.json()
예제 #2
0
class UndersideApi(object):
    session = sessions.BaseUrlSession(
        base_url='https://underside.tjcache.pw/api/v1/message/')
    trans_table = str.maketrans('\u200e\u200b', '01')
    encr_table = str.maketrans('01', '\u200e\u200b')

    @staticmethod
    def bin2ascii(bin: str) -> str:
        n = int(bin, 2)
        return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()

    @staticmethod
    def ascii2bin(string: str) -> str:
        return bin(int.from_bytes(string.encode(), 'big')).replace('b', '')

    def code_decryption(self, json: dict) -> str:
        return self.bin2ascii(json.get('code', '').translate(self.trans_table))

    def code_encryption(self, code: str) -> str:
        return self.ascii2bin(code).translate(self.encr_table)

    def encode(self, message: str) -> str:
        request = self.session.post('encode/', json={'message': message})
        return self.code_decryption(request.json())

    def decode(self, code: str) -> str:
        request = self.session.post('decode/',
                                    json={'code': self.code_encryption(code)})
        return request.json().get('text', '')
예제 #3
0
파일: cli.py 프로젝트: SrzStephen/Aicamera
def to_http(config, base_url):
    start = time()
    retry_strategy = Retry(total=5,
                           status_forcelist=[500, 502, 503, 504],
                           method_whitelist=["POST"],
                           backoff_factor=3)
    http = sessions.BaseUrlSession(base_url=BASE_URL())
    adaptor = HTTPAdapter(max_retries=retry_strategy)
    http.adapters = {"https://": adaptor, "http://": adaptor}

    while not quitevent.is_set():
        for item in config.generator:
            item['image'] = image_to_base64(item['image'])
            header = dict(sent_from=gethostname(),
                          uptime=str(time() - start),
                          device_name=str(config.device_name))
            data = dict(device_name=item['device_name'],
                        timestamp=datetime.utcnow().replace(
                            tzinfo=pytz.utc).isoformat(),
                        confidence=item['is_bad'],
                        coordinates=[item['lat'], item['lon']],
                        photo_data=item['image'])
            http.post(f"{base_url}/dev/upload",
                      data=dumps(data),
                      headers=header)
예제 #4
0
    def __init__(self,
                 base,
                 token=None,
                 username=None,
                 password=None,
                 _getuser=_getuser_default,
                 _getpass=_getpass_default,
                 retries=3,
                 insecure=False):
        self.lock = Lock()

        self.base = base.rstrip('/')
        self.token = token or os.environ.get('CDROUTER_API_TOKEN')
        self.username = username
        self.password = password
        self._getuser = _getuser
        self._getpass = _getpass
        self.retries = retries
        self.insecure = insecure

        if insecure:
            # disable annoying InsecureRequestWarning
            requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        self.session = sessions.BaseUrlSession(base_url=self.base + self.BASE)

        #: :class:`configs.ConfigsService <configs.ConfigsService>` object
        self.configs = ConfigsService(self)
        #: :class:`devices.DevicesService <devices.DevicesService>` object
        self.devices = DevicesService(self)
        #: :class:`attachments.AttachmentsService <attachments.AttachmentsService>` object
        self.attachments = AttachmentsService(self)
        #: :class:`jobs.JobsService <jobs.JobsService>` object
        self.jobs = JobsService(self)
        #: :class:`packages.PackagesService <packages.PackagesService>` object
        self.packages = PackagesService(self)
        #: :class:`results.ResultsService <results.ResultsService>` object
        self.results = ResultsService(self)
        #: :class:`testresults.TestResultsService <testresults.TestResultsService>` object
        self.tests = TestResultsService(self)
        #: :class:`annotations.AnnotationsService <annotations.AnnotationsService>` object
        self.annotations = AnnotationsService(self)
        #: :class:`captures.CapturesService <captures.CapturesService>` object
        self.captures = CapturesService(self)
        #: :class:`highlights.HighlightsService <highlights.HighlightsService>` object
        self.highlights = HighlightsService(self)
        #: :class:`imports.ImportsService <imports.ImportsService>` object
        self.imports = ImportsService(self)
        #: :class:`exports.ExportsService <exports.ExportsService>` object
        self.exports = ExportsService(self)
        #: :class:`history.HistoryService <history.HistoryService>` object
        self.history = HistoryService(self)
        #: :class:`system.SystemService <system.SystemService>` object
        self.system = SystemService(self)
        #: :class:`tags.TagsService <tags.TagsService>` object
        self.tags = TagsService(self)
        #: :class:`testsuites.TestsuitesService <testsuites.TestsuitesService>` object
        self.testsuites = TestsuitesService(self)
        #: :class:`users.UsersService <users.UsersService>` object
        self.users = UsersService(self)
예제 #5
0
class Renderer:
    session = sessions.BaseUrlSession('https://dl.dropboxusercontent.com')
    url = '/s/sg48j6iuoc819jm/Jason R. Coombs resume.xml'

    def __init__(self, url=None):
        if url:
            self.url = url

    def get_transform_path(self, output_name):
        path_tmpl = 'resume-1.5.1/xsl/output/{output_name}.xsl'
        path = path_tmpl.format(**locals())
        here = os.path.dirname(__file__)
        return os.path.join(here, 'static', path)

    def html(self):
        transform_path = self.get_transform_path('us-html')
        transform = etree.XSLT(etree.parse(open(transform_path)))
        resp = self.session.get(self.url)
        src = etree.fromstring(resp.content)
        return str(transform(src))

    def pdf(self):
        "use subprocess and fop to render the output"
        cmd = [
            'fop', '-xml', '-', '-xsl',
            self.get_transform_path('us-letter'), '-'
        ]
        resp = self.session.get(self.url)
        proc = subprocess.Popen(cmd,
                                stdout=subprocess.PIPE,
                                stdin=subprocess.PIPE)
        stdout, stderr = proc.communicate(resp.content)
        return stdout
예제 #6
0
def main():
    pub_api_session = sessions.BaseUrlSession(base_url=PUBLIC_API)
    priv_api_session = None
    if args.cookies:
        if os.path.isfile(args.cookies):
            priv_api_session = create_priv_api_session(cookie_jar_path=args.cookies)
        else:
            print_log("openrec-dl", f"could not find cookies file \'{args.cookies}\', continuing without cookies")
    elif args.username and args.password:
        cookies = get_cookies_from_username_password(args.username, args.password)
        priv_api_session = create_priv_api_session(cookie_jar=cookies)

    if args.version:
        print(VERSION_STRING)
        return
    if len(args.links) == 0:
        parser.print_usage()
    elif not os.path.isdir(args.directory):
        os.makedirs(args.directory)
    for link in args.links:
        # is openrec link
        openrec_m = re.search(OPENREC, link)
        id_m = re.search(VALID_LIVE_ID, link)
        if openrec_m:
            t = openrec_m.group("type")
            if t == "user":
                dl_channel(pub_api_session, priv_api_session, openrec_m.group("id"))
            elif t == "live":
                dl_movie(pub_api_session, priv_api_session, openrec_m.group("id"))
            else:
                print_log("openrec", f"unknown link type \'{t}\'")
        elif id_m:
            dl_movie(pub_api_session, priv_api_session, id_m.group("id"))
        else:
            print_log("openrec", f"invalid link or id \'{link}\'")
예제 #7
0
 def get_session(cls):
     session = sessions.BaseUrlSession('https://api.github.com/repos/')
     session.headers.update(
         Accept='application/vnd.github.v3+json',
         Authorization=f'token {cls.load_token()}',
     )
     return session
예제 #8
0
    def __init__(
        self,
        stitch_api_key: str,
        stitch_client_id: int,
        stitch_auth_user: str,
        stitch_auth_password: str,
        stitch_blacklist_sources: str = None,
    ) -> None:
        self.stitch_api_key = stitch_api_key
        self.stitch_client_id = stitch_client_id
        self.stitch_auth_user = stitch_auth_user
        self.stitch_auth_password = stitch_auth_password

        self.client = sessions.BaseUrlSession(base_url=constants.API_URL)
        self.client.hooks["response"] = [assert_status_hook]

        self.headers = {
            'Accept': 'application/json',
            'Origin': 'https://app.stitchdata.com',
            'User-Agent': constants.DEVICE_SETTINGS['user_agent'],
            'Content-Type': 'application/json',
            'Authorization': 'Bearer %s' % stitch_api_key
        }
        self._logged_in_internal = False
        self.write_blacklist = None
        self._parse_blacklist_config(stitch_blacklist_sources)
예제 #9
0
def set_image(image):
    with sessions.BaseUrlSession(base_url=address) as s:
        login_page = s.get('users/sign_in')

        login_payload = get_auth_payload(login_page)

        login_responce = s.post('users/sign_in', data=login_payload)

        appearance_page = s.get('admin/appearance')

        appearance_payload = process_appearance_page(appearance_page)

        # We COULD use python-magic and libmagic to get the mime type... or I can just make it png.
        # http://stackoverflow.com/a/2753385
        appearance_payload['appearance[header_logo]'] = (image.name,
                                                         image.open(mode='rb'),
                                                         'image/png')

        logging.info(image.name)

        appearance_payload = MultipartEncoder(fields=appearance_payload)

        appearance_responce = s.post(
            'admin/appearance',
            data=appearance_payload,
            headers={'Content-Type': appearance_payload.content_type})
예제 #10
0
    def __init__(self,
                 token=None,
                 base_url=r'https://developer.api.autodesk.com/',
                 timeout=1):
        """
        Initialize the ForgeApi class and optionally attach an authentication token for the Autodesk Forge API.

        Args:
            token (str): Authentication token for Autodesk Forge API.
            base_url (str, optional): Base URL for calls to the forge API.
                Defaults to r'https://developer.api.autodesk.com/'
            timeout (float, optional): Default timeout for API calls. Defaults to 1.

        Returns:
            None.

        """
        self.token = token
        self.http = sessions.BaseUrlSession(base_url)
        self.http.hooks['response'] = [
            lambda response, *args, **kwargs: response.raise_for_status()
        ]
        retries = Retry(total=6,
                        backoff_factor=1,
                        status_forcelist=[429, 500, 502, 503, 504])
        adapter = TimeoutHttpAdapter(timeout=timeout, max_retries=retries)
        self.http.mount("https://", adapter)
        self.http.mount("http://", adapter)
예제 #11
0
    def __init__(
        self,
        base_url,
        user,
        password,
        timeout=HTTP_DEFAULT_TIMEOUT,
        max_retries=HTTP_DEFAULT_MAX_RETRIES,
        cache_maxsize=CACHE_DEFAULT_MAXSIZE,
        cache_ttl=CACHE_DEFAULT_TTL,
    ):
        self._user = user
        self._password = password

        self._http = sessions.BaseUrlSession(base_url=base_url)
        self._cache_maxsize = cache_maxsize
        self._cache_ttl = cache_ttl

        # Retry strategy for http requests
        retries = Retry(
            total=max_retries,
            status_forcelist=[429, 500, 502, 503, 504],
            method_whitelist=[
                "HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE", "POST"
            ],
            backoff_factor=
            1,  # this will do `sleep({backoff factor} * (2 ** ({number of retries} - 1)))`
        )

        # Include timeout for http requests
        adapter = TimeoutHTTPAdapter(timeout=timeout, max_retries=retries)

        self._http.mount("https://", adapter)
        self._http.mount("http://", adapter)
예제 #12
0
    def __init__(
        self,
        api_key: str,
        query_dir: Optional[str] = None,
        base_url: Optional[str] = r"https://classic.warcraftlogs.com:443/v1/",
        timeout: Optional[int] = 1,
    ) -> None:
        """
        Initialize the WCLApi class and optionally attach an authentication token.

        Args:
            api_key (str): Authentication api_key.
            query_dir (str, optional): Path to the directory where queries should be
                stored.
            base_url (str, optional): Base URL for calls to the API.
                Defaults to r'https://classic.warcraftlogs.com:443/v1/'
            timeout (float, optional): Default timeout for API calls. Defaults to 1.

        Returns:
            None.

        """
        assert api_key is not None, "Please enter an api_key"
        self.api_key = api_key
        self.query_dir = query_dir
        self.http = sessions.BaseUrlSession(base_url)
        self.http.hooks["response"] = [
            lambda response, *args, **kwargs: response.raise_for_status()
        ]
        retries = Retry(total=6,
                        backoff_factor=1,
                        status_forcelist=[429, 500, 502, 503, 504])
        adapter = TimeoutHttpAdapter(timeout=timeout, max_retries=retries)
        self.http.mount("https://", adapter)
        self.http.mount("http://", adapter)
예제 #13
0
 def test_override_base(self):
     session = sessions.BaseUrlSession('https://www.google.com')
     recorder = get_betamax(session)
     with recorder.use_cassette('simple_get_request'):
         response = session.get('https://httpbin.org/get')
     response.raise_for_status()
     assert response.json()['headers']['Host'] == 'httpbin.org'
예제 #14
0
 def __init__(self, playlist_base):
     self.m3u8_session = sessions.BaseUrlSession(base_url=playlist_base)
     adapter = requests.adapters.HTTPAdapter(pool_connections=5, pool_maxsize=5, max_retries=10)
     self.m3u8_session.mount('http://', adapter)
     self.m3u8_session.mount('https://', adapter)
     self.success = True
     self.failed_list = []
     self.completed = {}
예제 #15
0
 def __init__(self, username, password):
     super(MobileCommonsIntegration, self).__init__()
     if username and password:
         self.mc_api = sessions.BaseUrlSession(
             base_url='https://secure.mcommons.com')
         self.mc_api.auth = HTTPBasicAuth(username, password)
     else:
         raise Exception('unable to authenticate to MobileCommons')
예제 #16
0
    def __init__(self, host, api_key):
        super(Jackett, self).__init__()
        self._api_key = api_key
        self._caps = {}

        self._session = sessions.BaseUrlSession(
            base_url=urljoin(host, "/api/v2.0/indexers/"))
        self.get_caps()
 def __init__(self, config):
     self.url = config["url"]
     self.session = sessions.BaseUrlSession(base_url=self.url)
     if "token" in config:
         self.session.headers.update({"X-Device-Token": config["token"]})
     else:
         self.session.headers.update({"X-Device-Token": config['environ']})
     self.min_delay = 0.6
예제 #18
0
파일: main.py 프로젝트: sml1991/fmpa
        def endpt(self, url):

            baseurl = "https://financialmodellingprep.com/api/v3/"
            http = sessions.BaseUrlSession(base_url=baseurl)
            path = (url + "?apikey={}").format(self.symb, self.getkey)
            #print(path)
            result = http.get(path).text
            return json.loads(result)
예제 #19
0
	def __init__(self, host, username, password):
		self.session = sessions.BaseUrlSession(base_url=urljoin(host, 'api/'))
		token = codecs.decode(b64encode(codecs.encode(f'{username}:{password}')))

		self.session.headers.update({
			'User-Agent': 'mautic2keycloak',
			'Accept': 'application/json',
			'Authorization': f'Basic {token}'
		})
 def __init__(self):
     self.api_key = getpass.getpass('api_key: ')
     self.endpoint = sessions.BaseUrlSession(
         base_url="https://content.guardianapis.com")
     adapter = HTTPAdapter(
         max_retries=Retry(total=3,
                           backoff_factor=1,
                           status_forcelist=[429, 500, 502, 503, 504]))
     self.endpoint.mount('https://', adapter)
예제 #21
0
def cloudflare(ctx, email, token):
    client = sessions.BaseUrlSession(base_url="https://api.cloudflare.com")
    client.hooks["response"] = [exit_hook]
    client.headers.update({
        "x-auth-email": email,
        "x-auth-key": token,
    })

    ctx.obj = {"client": client}
예제 #22
0
def drop_dbs() -> None:
    with sessions.BaseUrlSession(base_url="http://localhost:5984/") as client:
        client.auth = ("admin", "adminadmin")

        all_dbs = client.get("_all_dbs").json()
        for db in all_dbs:
            if db == MASTER_DB:
                continue
            response = client.delete(f"{db}")
            assert response.status_code == 200
예제 #23
0
파일: user.py 프로젝트: AGhost-7/stockpiler
 def __init__(self, email, password):
     self.id = None
     self.email = email
     self.password = password
     self.credentials = {
         'email': email,
         'password': password
     }
     self.requests = sessions.BaseUrlSession(
         base_url='http://localhost:5000')
예제 #24
0
def log_all():
    def log_request(resp, *args, **kwargs):
        #print(f"Hook: {resp.url}")
        data = dump.dump_all(resp)
        print(data.decode())

    session = sessions.BaseUrlSession("https://httpbin.org")
    session.hooks['response'] = [
        log_request,
    ]
    session.get("/ip")
예제 #25
0
def get_endpoint(endpoint, params=None):
    with sessions.BaseUrlSession(
            base_url="https://api.covid19api.com/") as session:
        try:
            response = session.get(endpoint)
            response.raise_for_status()
            logging.info(f"Request successful for endpoint={endpoint}.")
        except requests.exceptions.HTTPError as e:
            logging.error(f"{e}. Retrying...")
            response = get_endpoint(endpoint)
    return response
예제 #26
0
def teams_session():
    client_id = ge('client_id')
    authority_id = ge('authority_id')
    client_credential = ge('client_credential')

    BASE_URL = "https://graph.microsoft.com/beta/"
    ms_s = sessions.BaseUrlSession(base_url=BASE_URL)

    app = ConfidentialClientApplication(
        client_id=client_id,
        authority=f"https://login.microsoftonline.com/{authority_id}",
        client_credential=client_credential)

    result = app.acquire_token_for_client(
        scopes=["https://graph.microsoft.com/.default"])
    token = result['access_token']
    sessions.BaseUrlSession(base_url='https://graph.microsoft.com/beta/')

    ms_s.headers.update({"Authorization": f"Bearer {token}"})
    return ms_s
예제 #27
0
 def __init__(self, domain, api_key):
     super(RogueIntegration, self).__init__()
     if api_key:
         self.rogue_session = sessions.BaseUrlSession(base_url='https://' +
                                                      domain)
         self.rogue_session.headers = {
             'X-DS-Importer-API-Key': api_key,
             'Accept': 'application/json'
         }
     else:
         raise Exception('unable to authenticate to Rogue')
예제 #28
0
def build_http(customer, headers):
    """Build an HTTP object with backoff/retry."""
    retry_strategy = Retry(
        total=7,
        status_forcelist=[429, 500, 502, 503, 504],
        backoff_factor=1,
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    base_url = f'https://www.googleapis.com/admin/directory/v1.1beta1/customer/{customer}/'
    http_client = sessions.BaseUrlSession(base_url=base_url)
    http_client.mount("https://", adapter)
    http_client.headers.update(headers)
    return http_client
예제 #29
0
def retry():
    session = sessions.BaseUrlSession("http://httpstat.us")
    session.verify = False

    retry_strategy = Retry(total=3,
                           status_forcelist=[429, 500, 502, 503, 504],
                           allowed_methods=["HEAD", "GET", "OPTIONS"],
                           backoff_factor=1)
    adapter = HTTPAdapter(max_retries=retry_strategy)

    session.mount("http://", adapter)
    session.mount("https://", adapter)
    session.get("/500")
예제 #30
0
def cp_native_server(request):
    """A native server."""
    class Root(object):
        @cherrypy.expose
        def index(self):
            return 'Hello World!'

    cls = cherrypy._cpnative_server.CPHTTPServer
    cherrypy.server.httpserver = cls(cherrypy.server)

    cherrypy.tree.mount(Root(), '/')
    cherrypy.engine.start()
    request.addfinalizer(cherrypy.engine.stop)
    url = 'http://localhost:{cherrypy.server.socket_port}'.format(**globals())
    return sessions.BaseUrlSession(url)