Example #1
1
def _http_call(the_url, method, authorization, **kw):
    """
    send an http request and expect to return a json object if no error.
    """

    params = None
    boundary = None
    if method == _HTTP_UPLOAD:
        (params, boundary) = _encode_multipart(**kw)
    else:
        params = _encode_params(**kw)
    http_url = "%s?%s" % (the_url, params) if method == _HTTP_GET else the_url
    http_body = None if method == _HTTP_GET else params
    req = urllib2.Request(http_url, data=http_body)
    if authorization:
        req.add_header("Authorization", "OAuth2 %s" % authorization)
    if boundary:
        req.add_header("Content-Type", "multipart/form-data; boundary=%s" % boundary)
    try:
        resp = urllib2.urlopen(req)
        body = resp.read()
        r = json.loads(body, object_hook=_obj_hook)
        if hasattr(r, "error_code"):
            raise APIError(r.error_code, r.get("error", ""), r.get("request", ""))
        return r
    except urllib2.HTTPError, e:
        r = json.loads(e.read(), object_hook=_obj_hook)
        if hasattr(r, "error_code"):
            raise APIError(r.error_code, r.get("error", ""), r.get("request", ""))
        raise
Example #2
1
    def test_CrashPairsByCrashId(self, rget):
        url = reverse("api:model_wrapper", args=("CrashPairsByCrashId",))
        response = self.client.get(url)
        eq_(response.status_code, 400)
        dump = json.loads(response.content)
        ok_(dump["errors"]["uuid"])
        ok_(dump["errors"]["hang_id"])

        def mocked_get(url, **options):
            return Response(
                """
              {
                "hits": [{"guess": "work"}],
                "total": 1
              }
            """
            )
            raise NotImplementedError(url)

        rget.side_effect = mocked_get

        response = self.client.get(url, {"uuid": "123", "hang_id": "987"})
        eq_(response.status_code, 200)
        dump = json.loads(response.content)
        ok_(dump["hits"])
        ok_(dump["total"])
def fetchsamples():

    tags = ["ptths"]  # , "mtgths", "ptths", "gpdc", "mtg"]
    #  tags = [InputTag]
    for x in tags:
        url = "https://api.twitter.com/1.1/search/tweets.json?count=100&q="
        url = url + x
        parameters = []
        response = twitterreq(url, "GET", parameters)
        # print response.read()
        response = response.read()
        print response
        data = json.loads(response)
        MetaData = data["search_metadata"]
        maxid = str(MetaData["max_id"])
        # 	statuses = len(data["statuses"])
        # 	print statuses
        # 	print maxid
        for i in range(160):
            url = "https://api.twitter.com/1.1/search/tweets.json?count=100&q="
            url = url + x
            url = url + "&max_id=" + maxid
            # 	print url
            parameters = []
            response = twitterreq(url, "GET", parameters)
            # 	print response.read()
            response = response.read()
            print response
            # 		statuses = json.loads(response)["statuses"]
            # 		print statuses
            data = json.loads(response)
            MetaData = data["search_metadata"]
            maxid = str(MetaData["max_id"])
    def test_run_generates_and_show_results_page(self):
        runner, port = self.create_runner_and_setup_results_template(
            args=["--output-json-path=/mock-checkout/output.json"]
        )
        page_shown = []
        port.show_results_html_file = lambda path: page_shown.append(path)
        filesystem = port.host.filesystem
        self._test_run_with_json_output(runner, filesystem)

        expected_entry = {
            "timestamp": 123456789,
            "results": self._event_target_wrapper_and_inspector_results,
            "webkit-revision": "5678",
            "branch": "webkit-trunk",
        }

        self.maxDiff = None
        json_output = port.host.filesystem.read_text_file("/mock-checkout/output.json")
        self.assertEqual(json.loads(json_output), [expected_entry])
        self.assertEqual(
            filesystem.read_text_file("/mock-checkout/output.html"),
            'BEGIN<script src="/test.checkout/some.js"></script><script src="/test.checkout/other.js"></script>'
            "<script>%s</script>END" % json_output,
        )
        self.assertEqual(page_shown[0], "/mock-checkout/output.html")

        self._test_run_with_json_output(runner, filesystem)
        json_output = port.host.filesystem.read_text_file("/mock-checkout/output.json")
        self.assertEqual(json.loads(json_output), [expected_entry, expected_entry])
        self.assertEqual(
            filesystem.read_text_file("/mock-checkout/output.html"),
            'BEGIN<script src="/test.checkout/some.js"></script><script src="/test.checkout/other.js"></script>'
            "<script>%s</script>END" % json_output,
        )
Example #5
1
    def test_CorrelationsSignatures(self, rget):
        def mocked_get(url, **options):
            assert "correlations/signatures" in url
            return Response(
                """
                {
                    "hits": ["FakeSignature1",
                             "FakeSignature2"],
                    "total": 2
                }
            """
            )

        rget.side_effect = mocked_get

        url = reverse("api:model_wrapper", args=("CorrelationsSignatures",))
        response = self.client.get(url)
        dump = json.loads(response.content)
        ok_(dump["errors"]["product"])
        ok_(dump["errors"]["version"])
        ok_(dump["errors"]["report_type"])

        response = self.client.get(
            url,
            {
                "platforms": "Windows NT+Mac OS OX",
                "product": "WaterWolf",
                "version": "1.0",
                "report_type": "core-counts",
            },
        )
        eq_(response.status_code, 200)
        dump = json.loads(response.content)
        eq_(dump["hits"], [u"FakeSignature1", u"FakeSignature2"])
        eq_(dump["total"], 2)
Example #6
1
    def test_twitter_login_registration(self, verify_credentials):
        twitter_data = TWITTER_DATA.copy()
        twitter_data["id"] = uuid.uuid4().hex
        verify_credentials.return_value = type("U", (object,), {"AsDict": lambda s: twitter_data})()

        with self.app.test_client() as client:
            token_key, token_secret = "kkkkkkkk", "sssssss"
            for registered in True, False:
                r = client.post(
                    "/ws/login/external/",
                    headers=[get_client_auth_header()],
                    data=dict(external_system="twitter", external_token=token_key + ":" + token_secret),
                )
                self.assertEquals(200, r.status_code)
                creds = json.loads(r.data)
                self.assertEquals(creds["registered"], registered)

            r = client.get(
                creds["resource_url"], headers=[("Authorization", creds["token_type"] + " " + creds["access_token"])]
            )
            data = json.loads(r.data)
            self.assertEquals(data["username"], TWITTER_DATA["screen_name"])
            self.assertEquals(data["display_name"], TWITTER_DATA["name"])
            self.assertTrue(data["avatar_thumbnail_url"])

            token = ExternalToken.query.filter_by(user=creds["user_id"]).one()
            self.assertEquals(token.external_uid, twitter_data["id"])
            self.assertIn("read", token.permissions)
Example #7
1
    def post(self):
        # array of time strings: "08:00 AM"
        bannedTimes = json.loads(self.request.get("bannedTimes"))
        # array of all bannedDays checkboxes (booleans)
        bannedDays = json.loads(self.request.get("bannedDays"))

        badIvals = []
        # each 2 time strings corresponds to 5 bannedDays checkboxes
        for dayBools, times in zip(misc.iterGroups(bannedDays, 5), misc.iterGroups(bannedTimes, 2)):
            badIvals += parseJSInterval(times, dayBools)

        subCodes = json.loads(self.request.get("subCodes"))
        nums = json.loads(self.request.get("nums"))
        curCRNs = json.loads(self.request.get("curCRNs"))

        classes = [t + (year, season) for t in zip(subCodes, nums)]

        try:
            clsToSections = courses.planSchedule(classes, badIvals, curCRNs)
        except:
            self.response.out.write(json.dumps({}))
        else:
            for cls, sections in clsToSections.items():
                clsToSections[cls[0] + " " + str(cls[1])] = sections
                del clsToSections[cls]

                for sec in sections:
                    sec["Intervals"] = [courses.reprInterval(i) for i in sec["Intervals"]]

            self.response.out.write(json.dumps(clsToSections))
Example #8
0
def urls(request):
    if request.method == "GET":
        urls = list(URL.objects.values())
        urls_json = json.dumps(urls)
        return HttpResponse(urls_json, content_type="application/json")
    elif request.method == "POST":
        try:
            payload = json.loads(request.body)
        except:
            return mock_server_error("Fail to unmarshal json string")
        if "name" not in payload or "pattern" not in payload:
            return mock_server_error("Lacking required field")
        try:
            re.compile(payload["pattern"])
        except:
            return mock_server_error("invalid regular expression")
        url = URL(name=payload["name"], pattern=payload["pattern"])
        url.save()
        return mock_server_success()
    elif request.method == "DELETE":
        try:
            payload = json.loads(request.body)
        except:
            return mock_server_error("Fail to unmarshal json string")
        if "id" not in payload:
            return mock_server_error("Lacking required field:id")
        try:
            url = URL.objects.get(id=int(payload["id"]))
        except:
            return mock_server_error("URL not found")
        url.delete()
        return mock_server_success()
    else:
        return mock_server_error("HTTP method not supported.")
Example #9
0
    def get_collection_id_by_dc_identifier(self, dcid, name=None, create=False, public=False):
        """Find an Omeka collection by name and cache the results. Does not deal with collections with the same title"""
        element_id = self.getElementId(self.dublinCoreID, "Identifier")
        title_id = self.getElementId(self.dublinCoreID, "Title")

        def get_identifier(collection):
            for t in collection["element_texts"]:
                if t["element"]["id"] == element_id:
                    self.collections_by_dc_identifier[t["text"]] = collection

        if self.collections_by_dc_identifier == {}:
            response, content = self.get("collections")
            collections_data = json.loads(content)
            for collection in collections_data:
                get_identifier(collection)

        if not dcid in self.collections_by_dc_identifier and create:
            if name == None:
                name = dcid
            element_text1 = {"html": False, "text": name}
            element_text1["element"] = {"id": title_id}

            element_text2 = {"html": False, "text": dcid}
            element_text2["element"] = {"id": element_id}

            response, content = self.post(
                "collections", json.dumps({"element_texts": [element_text1, element_text2], "public": public})
            )
            collection = json.loads(content)
            get_identifier(collection)

        return self.collections_by_dc_identifier[dcid]["id"] if dcid in self.collections_by_dc_identifier else None
Example #10
0
    def getCollectionId(self, name, create=False, public=False):
        """Find an Omeka collection by name and cache the results. Does not deal with collections with the same title"""

        def getTitle(collection):
            for t in collection["element_texts"]:
                if t["element"]["name"] == "Title":
                    self.collections[t["text"]] = collection

        if self.collections == {}:
            response, content = self.get("collections")
            collections_data = json.loads(content)
            for collection in collections_data:
                getTitle(collection)

        if not name in self.collections and create:
            title_id = self.getElementId(self.dublinCoreID, "Title")
            element_text = {"html": False, "text": name}
            element_text["element"] = {"id": title_id}

            response, content = self.post(
                "collections", json.dumps({"element_texts": [element_text], "public": public})
            )
            collection = json.loads(content)
            getTitle(collection)

        return self.collections[name]["id"] if name in self.collections else None
    def _make_request(self, verb, url, body_dict):
        url = "https://api.itbit.com/v1" + url
        nonce = self._get_next_nonce()
        timestamp = self._get_timestamp()

        if verb in ("PUT", "POST"):
            json_body = json.dumps(body_dict)
        else:
            json_body = ""

        signer = MessageSigner()
        signature = signer.sign_message(self.secret, verb, url, json_body, nonce, timestamp)

        auth_headers = {
            "Authorization": self.clientKey + ":" + signature,
            "X-Auth-Timestamp": timestamp,
            "X-Auth-Nonce": nonce,
            "Content-Type": "application/json",
        }
        if json_body == "":
            req = urllib2.Request(url=url, headers=auth_headers)
        else:
            req = urllib2.Request(url=url, data=json_body, headers=auth_headers)
        try:
            response = urllib2.urlopen(req)
            r = json.loads(response.read())
        except urllib2.HTTPError as e:
            error = json.loads(e.read())
            log.error("httpstatus: " + str(e.code) + " code: " + str(error["code"]) + " message: " + error["message"])
            r = None
        return r
Example #12
0
    def __init__(self, r):
        self.body = None

        if isinstance(r, bytes):
            r = r.decode("utf-8")

        if isinstance(r, str_instances):
            lines = r.split("\n")
            self.code = int(lines[0])
            if self.code != 204:
                self.body = json.loads("\n".join(lines[1:]))
        elif hasattr(r, "code"):
            # Hopefully this is an HTTPError
            self.code = r.code
            if self.code != 204:
                self.body = json.loads(r.read().decode("utf-8"))
        elif hasattr(r, "reason"):
            # Hopefully this is a URLError
            # horrible hack, but lots of other stuff checks the response code :/
            self.code = 500
            self.body = r.reason
        else:
            # WFIO
            self.code = 500
            self.body = r
        msg.debug("code: %s" % self.code)
Example #13
0
    def execModules(self, session, session_ip, beef):
        session_browser = beef.sessionInfo(session)["BrowserName"]
        session_os = beef.sessionInfo(session)["OsName"]

        if len(self.All_modules) > 0:
            logging.info("%s >> sending generic modules" % session_ip)
            for module, options in self.All_modules.items():
                mod_id = beef.getModid(module)
                resp = beef.runModule(session, mod_id, json.loads(options))
                if resp["success"] == "true":
                    logging.info("%s >> sent module %s" % (session_ip, mod_id))
                else:
                    logging.info("%s >> ERROR sending module %s" % (session_ip, mod_id))
                sleep(0.5)

        logging.info("%s >> sending targeted modules" % session_ip)
        for os in self.Targeted_modules:
            if (os in session_os) or (os == session_os):
                browsers = self.Targeted_modules[os]
                if len(browsers) > 0:
                    for browser in browsers:
                        if browser == session_browser:
                            modules = self.Targeted_modules[os][browser]
                            if len(modules) > 0:
                                for module, options in modules.items():
                                    mod_id = beef.getModid(module)
                                    resp = beef.runModule(session, mod_id, json.loads(options))
                                    if resp["success"] == "true":
                                        logging.info("%s >> sent module %s" % (session_ip, mod_id))
                                    else:
                                        logging.info("%s >> ERROR sending module %s" % (session_ip, mod_id))
                                    sleep(0.5)
def json_dict_bucket_parse(json_str, conversion_dict, bucket_field, bucket_interval, bucket_type, ensure_ascii=False):
    """Parses a json based on the `conversion_dict` and adds two fields 
	`bucket_start` and `bucket_end` based on the `bucket_field`

	Args:
		json_str: a string representation of the json message that needs to be
			transformed. Assumes the json_str is a json dictionary
		conversion_dict: a dictionary of the items to be parsed. It is in the
			the format..
			{
				'new_key': 'json_str_key',
				'new_key2': 'json_str_key2',
				'new_key3': 'json_str_key3',
			}
			The keys for the dictionary will be the new keys for the transform-
			ed json and the values associated with each key indicate the key 
			from the original json whose value will now be associated with the
			new keys. return_json[new_key]=json_str[json_str_key]. See doctest
			for example
		bucket_field: an epoch (int) field within the json string that will be
			used to for the buckets

	Returns:
		A new dictionary object parsed with the objects specified and  

	"""
    new_json = json_dict_parse(json_str, conversion_dict)
    new_json_dict = json.loads(new_json)
    old_json_dict = json.loads(json_str)

    cast = TS_LEGEND[bucket_type]
    bucket_start, bucket_end = cast(old_json_dict[bucket_field], bucket_interval)
    new_json_dict["bucket_start"] = bucket_start
    new_json_dict["bucket_end"] = bucket_end
    return json.dumps(new_json_dict, ensure_ascii=ensure_ascii)
def read_patch_file(filename, region="na"):
    with open(filename) as f:
        patch_string = f.read()

    # If file is empty, fill its contents with new ones
    if "last_update" not in patch_string:
        this_patch = api.get_versions(region, reverse=False)[0]
        patch_dict = {region: {"current_patch": this_patch, "last_update": time.time()}}

        with open(filename, "w") as f:
            f.write(json.dumps(patch_dict))

    elif region not in patch_string:
        patch_dict = json.loads(patch_string)
        this_patch = api.get_versions(region, reverse=False)[0]
        patch_dict[region] = {"current_patch": this_patch, "last_update": time.time()}

        with open(filename, "w") as f:
            f.write(json.dumps(patch_dict))

    else:
        patch_dict = json.loads(patch_string)

        # If file is outdated (check every hour), update current patch
        if time.time() - patch_dict[region]["last_update"] > 3600:
            this_patch = api.get_versions(region, reverse=False)[0]
            patch_dict[region] = {"current_patch": this_patch, "last_update": time.time()}

            with open(filename, "w") as f:
                f.write(json.dumps(patch_dict))

    return patch_dict[region]["current_patch"]
Example #16
0
def GetInstitutions(page, name=None):
    if name is None:
        return json.loads(requests.get("%s?page=%d" % (RESTFUL_INSTITUTION, page)).text)
    else:
        q_value = '{"filters":[{"name":"name","op":"like","val":"%%%s%%"}]}' % name
        q_value = urllib.quote(q_value.encode("utf-8"))
        return json.loads(requests.get("%s?page=%d&q=%s" % (RESTFUL_INSTITUTION, page, q_value)).text)
Example #17
0
def GetBulletins(page, title=None):
    if title is None:
        return json.loads(requests.get("%s?page=%d" % (RESTFUL_BULLETIN, page)).text)
    else:
        q_value = '{"filters":[{"name":"title","op":"like","val":"%%%s%%"}]}' % title
        q_value = urllib.quote(q_value.encode("utf-8"))
        return json.loads(requests.get("%s?page=%d&q=%s" % (RESTFUL_BULLETIN, page, q_value)).text)
Example #18
0
    def get_identifier(collection):
        for t in collection["element_texts"]:
            if t["element"]["id"] == element_id:
                self.collections_by_dc_identifier[t["text"]] = collection

        if self.collections_by_dc_identifier == {}:
            response, content = self.get("collections")
            collections_data = json.loads(content)
            for collection in collections_data:
                get_identifier(collection)

        if not dcid in self.collections_by_dc_identifier and create:
            if name == None:
                name = dcid
            element_text1 = {"html": False, "text": name}
            element_text1["element"] = {"id": title_id}

            element_text2 = {"html": False, "text": dcid}
            element_text2["element"] = {"id": element_id}

            response, content = self.post(
                "collections", json.dumps({"element_texts": [element_text1, element_text2], "public": public})
            )
            collection = json.loads(content)
            get_identifier(collection)

        return self.collections_by_dc_identifier[dcid]["id"] if dcid in self.collections_by_dc_identifier else None
Example #19
0
    def also_prune_variant(self, also_prune, tippees=1):

        self.make_participant("test_tippee1")
        self.make_participant("test_tippee2")
        test_tipper = self.make_participant("test_tipper")

        data = [{"username": "test_tippee1", "amount": "1.00"}, {"username": "test_tippee2", "amount": "2.00"}]

        response = self.client.POST(
            "/test_tipper/tips.json", body=json.dumps(data), content_type="application/json", auth_as=test_tipper
        )

        assert response.code == 200
        assert len(json.loads(response.body)) == 2

        response = self.client.POST(
            "/test_tipper/tips.json?also_prune=" + also_prune,
            body=json.dumps([{"username": "test_tippee2", "amount": "1.00"}]),
            content_type="application/json",
            auth_as=test_tipper,
        )

        assert response.code == 200

        response = self.client.GET("/test_tipper/tips.json", auth_as=test_tipper)
        assert response.code == 200
        assert len(json.loads(response.body)) == tippees
    def test_rescoring_bad_unicode_input(self):
        """Generate a real failure in rescoring a problem, with an answer including unicode"""
        # At one point, the student answers that resulted in StudentInputErrors were being
        # persisted (even though they were not counted as an attempt).  That is not possible
        # now, so it's harder to generate a test for how such input is handled.
        problem_url_name = "H1P1"
        # set up an option problem -- doesn't matter really what problem it is, but we need
        # it to have an answer.
        self.define_option_problem(problem_url_name)
        self.submit_student_answer("u1", problem_url_name, [OPTION_1, OPTION_1])

        # return an input error as if it were a numerical response, with an embedded unicode character:
        expected_message = u"Could not interpret '2/3\u03a9' as a number"
        with patch("capa.capa_problem.LoncapaProblem.rescore_existing_answers") as mock_rescore:
            mock_rescore.side_effect = StudentInputError(expected_message)
            instructor_task = self.submit_rescore_all_student_answers("instructor", problem_url_name)

        # check instructor_task returned
        instructor_task = InstructorTask.objects.get(id=instructor_task.id)
        self.assertEqual(instructor_task.task_state, "SUCCESS")
        self.assertEqual(instructor_task.requester.username, "instructor")
        self.assertEqual(instructor_task.task_type, "rescore_problem")
        task_input = json.loads(instructor_task.task_input)
        self.assertFalse("student" in task_input)
        self.assertEqual(
            task_input["problem_url"],
            InstructorTaskModuleTestCase.problem_location(problem_url_name).to_deprecated_string(),
        )
        status = json.loads(instructor_task.task_output)
        self.assertEqual(status["attempted"], 1)
        self.assertEqual(status["succeeded"], 0)
        self.assertEqual(status["total"], 1)
Example #21
0
    def _real_extract(self, url):
        mobj = re.match(self._VALID_URL, url)
        embedCode = mobj.group("id")
        player_url = "http://player.ooyala.com/player.js?embedCode=%s" % embedCode
        player = self._download_webpage(player_url, embedCode)
        mobile_url = self._search_regex(r'mobile_player_url="(.+?)&device="', player, u"mobile player url")
        mobile_player = self._download_webpage(mobile_url, embedCode)
        videos_info = self._search_regex(
            r'var streams=window.oo_testEnv\?\[\]:eval\("\((\[{.*?}\])\)"\);', mobile_player, u"info"
        ).replace('\\"', '"')
        videos_more_info = self._search_regex(
            r'eval\("\(({.*?\\"promo\\".*?})\)"', mobile_player, u"more info"
        ).replace('\\"', '"')
        videos_info = json.loads(videos_info)
        videos_more_info = json.loads(videos_more_info)

        if videos_more_info.get("lineup"):
            videos = [
                self._extract_result(info, more_info)
                for (info, more_info) in zip(videos_info, videos_more_info["lineup"])
            ]
            return {
                "_type": "playlist",
                "id": embedCode,
                "title": unescapeHTML(videos_more_info["title"]),
                "entries": videos,
            }
        else:
            return self._extract_result(videos_info[0], videos_more_info)
Example #22
0
    def test_post_ajax(self):
        """
        POSTing with AJAX should return success/error JSON.
        """

        with self.activate("en-US"):
            # test AJAX POST with valid form data
            request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH="XMLHttpRequest")

            response = views.process_content_services_form(request, self.template, self.view)

            # decode JSON response
            resp_data = json.loads(response.content)

            self.assertEqual(resp_data["msg"], "ok")
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response._headers["content-type"][1], "application/json")
            ok_(self.requests_mock.called)

            # test AJAX POST with invalid form data
            request = self.factory.post(self.url, self.invalid_post_data, HTTP_X_REQUESTED_WITH="XMLHttpRequest")

            response = views.process_content_services_form(request, self.template, self.view)

            # decode JSON response
            resp_data = json.loads(response.content)

            self.assertEqual(resp_data["msg"], "Form invalid")
            self.assertEqual(response.status_code, 400)
            self.assertTrue("email" in resp_data["errors"])
            self.assertEqual(response._headers["content-type"][1], "application/json")
Example #23
0
    def dataReceived(self, payload):
        try:
            request_json = json.loads(payload)
            if isinstance(request_json, unicode):
                payload = ast.literal_eval(payload)
                request_json = json.loads(payload)

            message_id = str(request_json["request"]["id"])

            if request_json["request"]["command"] == "get_vendors":
                self.get_vendors(message_id)

            if request_json["request"]["command"] == "get_moderators":
                self.get_moderators(message_id)

            elif request_json["request"]["command"] == "get_homepage_listings":
                self.get_homepage_listings(message_id)

            elif request_json["request"]["command"] == "search":
                self.search(message_id, request_json["request"]["keyword"].lower())

            elif request_json["request"]["command"] == "send_message":
                self.send_message(
                    request_json["request"]["guid"],
                    request_json["request"]["handle"],
                    request_json["request"]["message"],
                    request_json["request"]["subject"],
                    request_json["request"]["message_type"],
                    request_json["request"]["recipient_key"],
                )

        except Exception as e:
            print "Exception occurred: %s" % e
def get_repos(path, git_path, api_uri, headers):
    """Return a list of repositories from the provided github api.

    :param path: ``str``
    :param git_path: ``str``
    :param api_uri: ``str``
    :param headers: ``dict``
    :return: ``list``
    """
    response, content = HTTP.request(path, "HEAD", headers=headers)

    if "link" in response:
        repo_content = []
        links = response["link"].split(",")
        pages = [i.replace(" ", "") for i in links if "last" in i]
        page_link = pages[0].split(";")[0]
        page_link = page_link.strip(">").strip("<")
        page_link = page_link.split("=")
        page_num = int(page_link[-1])
        for page in range(0, page_num):
            git_page_number = page + 1
            req_path = git_path % (api_uri, git_page_number)
            response, content = HTTP.request(req_path, "GET", headers=headers)
            for repo in json.loads(content):
                repo_content.append(repo)
        else:
            return repo_content
    else:
        response, content = HTTP.request(path, "GET", headers=headers)
        return json.loads(content)
Example #25
0
    def test_CommentsBySignature(self, rget):
        url = reverse("api:model_wrapper", args=("CommentsBySignature",))
        response = self.client.get(url)
        eq_(response.status_code, 400)
        dump = json.loads(response.content)
        ok_(dump["errors"]["signature"])

        def mocked_get(url, **options):
            if "crashes/comments" in url:
                return Response(
                    """
                {
                  "hits": [
                   {
                     "user_comments": "This is a comment",
                     "date_processed": "2012-08-21T11:17:28-07:00",
                     "email": "some@emailaddress.com",
                     "uuid": "469bde48-0e8f-3586-d486-b98810120830"
                    }
                  ],
                  "total": 1
                }
              """
                )
            raise NotImplementedError(url)

        rget.side_effect = mocked_get

        response = self.client.get(url, {"signature": "one & two"})
        eq_(response.status_code, 200)
        dump = json.loads(response.content)
        ok_(dump["hits"])
        ok_(dump["total"])
    def query_api(self):
        url = "https://api.airbnb.com/v2/listings/"
        url += str(self.lid)
        url += "?client_id=3092nxybyb0otqw18e8nh5nty&_format=v1_legacy_for_p3"

        r = json.loads(requests.get(url).text.encode("utf-8"))

        # API rate limit exceeded -- pause <wait_mins> minutes
        wait_mins = 10
        while "error_code" in r.keys() and r["error_code"] == 503:
            sys.stderr.write("API limit exceeded -- waiting ")
            for i in range(wait_mins):
                sys.stderr.write(str(wait_mins - i) + " more minutes... ")
                time.sleep(60)
            r = json.loads(requests.get(url).text.encode("utf-8"))
            sys.stderr.write("\n")

        # other error codes, e.g. searched too far back
        if "error_code" in r.keys():
            sys.stderr.write(str(self.lid))
            sys.stderr.write(str(r))
            sys.stderr.write("\n")
            return None

        return r
 def get_shapes(self, slide_number, storage_type=None, storage_name=None, folder=None):
     try:
         if self.file_name == "":
             raise Exception("Please Specify File Name")
         str_uri = (
             Product.base_product_uri + "/slides/" + self.file_name + "/slides/" + str(slide_number) + "/shapes"
         )
         if folder is not None:
             str_uri += "?folder=" + folder
         if storage_name is not None:
             str_uri += "&storage=" + storage_name
         signed_uri = Utils.sign(Utils(), str_uri)
         response_stream = Utils.process_command(Utils(), signed_uri, "GET", "", "")
         json_data = json.loads(response_stream)
         shapes = {}
         if json_data["Code"] == 200:
             shape = {}
             for json_data["ShapeList"]["Links"] in shape:
                 signed_uri = Utils.sign(Utils(), shape["Uri"]["Href"])
                 response_stream = Utils.process_command(Utils(), signed_uri, "GET", "", "")
                 data = json.loads(response_stream)
                 shapes = data
             return shapes
         else:
             return json_data
     except:
         raise
Example #28
0
    def testGetSubTests_InternalData_OnlyReturnedForAuthorizedUsers(self):
        # When the user has a an internal account, internal-only data is given.
        self.SetCurrentUser("foo@google.com")
        self._AddSampleData()

        # Set internal_only on a bot and top-level test.
        bot = ndb.Key("Master", "Chromium", "Bot", "win7").get()
        bot.internal_only = True
        bot.put()
        test = graph_data.Test.get_by_id("dromaeo", parent=bot.key)
        test.internal_only = True
        test.put()

        # Set internal_only and has_rows to true on two subtests.
        for name in ["dom", "jslib"]:
            subtest = graph_data.Test.get_by_id(name, parent=test.key)
            subtest.internal_only = True
            subtest.has_rows = True
            subtest.put()

        # All of the internal-only tests are returned.
        response = self.testapp.post("/list_tests", {"type": "sub_tests", "suite": "dromaeo", "bots": "Chromium/win7"})
        expected = {"dom": {"has_rows": True, "sub_tests": {}}, "jslib": {"has_rows": True, "sub_tests": {}}}
        self.assertEqual(expected, json.loads(response.body))

        # After setting the user to another domain, an empty dict is returned.
        self.SetCurrentUser("foo@yahoo.com")
        response = self.testapp.post("/list_tests", {"type": "sub_tests", "suite": "dromaeo", "bots": "Chromium/win7"})
        self.assertEqual({}, json.loads(response.body))
Example #29
0
    def test_basic_stage(self):
        res = self._test_url(self.iframe_install_url)
        allowed_origins = json.loads(res.context["allowed_origins"])
        self._test_trailing_slashes(allowed_origins)
        eq_(
            allowed_origins,
            [
                "app://packaged.marketplace.allizom.org",
                "app://marketplace.allizom.org",
                "https://marketplace.allizom.org",
                "app://tarako.marketplace.allizom.org",
                "https://hello.firefox.com",
                "https://call.firefox.com",
            ],
        )

        res = self._test_url(self.potatolytics_url)
        allowed_origins = json.loads(res.context["allowed_origins"])
        self._test_trailing_slashes(allowed_origins)
        eq_(
            allowed_origins,
            [
                "app://packaged.marketplace.allizom.org",
                "app://marketplace.allizom.org",
                "https://marketplace.allizom.org",
                "app://tarako.marketplace.allizom.org",
            ],
        )
Example #30
0
    def test_create_duplicate_alias_name(self):
        resource = model.Package.get("annakarenina").resources[0]
        data = {"resource_id": resource.id, "aliases": u"myalias"}
        postparams = "%s=1" % json.dumps(data)
        auth = {"Authorization": str(self.sysadmin_user.apikey)}
        res = self.app.post("/api/action/datastore_create", params=postparams, extra_environ=auth, status=200)
        res_dict = json.loads(res.body)
        assert res_dict["success"] is True

        # try to create another table with the same alias
        resource = model.Package.get("annakarenina").resources[1]
        data = {"resource_id": resource.id, "aliases": u"myalias"}
        postparams = "%s=1" % json.dumps(data)
        auth = {"Authorization": str(self.sysadmin_user.apikey)}
        res = self.app.post("/api/action/datastore_create", params=postparams, extra_environ=auth, status=409)
        res_dict = json.loads(res.body)
        assert res_dict["success"] is False

        # try to create an alias that is a resource id
        resource = model.Package.get("annakarenina").resources[1]
        data = {"resource_id": resource.id, "aliases": model.Package.get("annakarenina").resources[0].id}
        postparams = "%s=1" % json.dumps(data)
        auth = {"Authorization": str(self.sysadmin_user.apikey)}
        res = self.app.post("/api/action/datastore_create", params=postparams, extra_environ=auth, status=409)
        res_dict = json.loads(res.body)
        assert res_dict["success"] is False