pass query param as dictionary with many values in postman - django

I am using postman to hit my APIs. I have a question regarding sending query params through postman params. In my API i am getting params using services = request.GET.get('services') and then returning response for the services.
My Question is if have more than one service like 'A', 'B', 'C', then how can we send these services in params using postman?
views.py
class SomeAPIView(ModelViewSet):
def get_queryset(self):
services = self.request.GET.get('services')
print(services) # getting services
print(type(services)) #type is string
response_list = []
for service in services:
result = API(service=service)
response_list.append(result)
return response_list
I want get list of services and then iterate over that list to return response for that service.

It depending on how you would use this API entry in production.
At first, the good way in django is to use request.query_params to obtain query parameters. Also you must provide default value to get() method to avoid exceptions if there's no 'services' parameter passed.
Then, if your services parameter contain names or ids of some objects, you may just pass it with parameters in GET request as http://someurl?services=A,B,C, or within tab, named 'Params' in postman. So request.query_params.get('sevices', '') will return string, contains 'A,B,C'. Now you can split it by ',' like services_names = str.split(',').
Anyway, parameters of GET requests may return only str values.
According to your example, it may looks like:
class SomeAPIView(ModelViewSet):
def get_queryset(self):
services = self.request.query_params.get('sevices', '').split(',')
print(services) # getting services
print(type(services)) # now it will be List[str]
response_list = []
for service in services:
result = API(service=service)
response_list.append(result)
return response_list

Related

How to query parameters with API Gateway to lambda

I made a REST API with AWS Lambda+ API Gateway.
my API Gateway's Integration Request is LAMBDA_PROXY Type,
and I use params in Lambda like this. ( myparam is list type)
def lambda_handler(event, context):
# TODO implement
try:
myparam = event['multiValueQueryStringParameters']['param1']
#...
I tested my REST API in python like this.
url = 'https://***.amazonaws.com/default/myAPI'
param = {'param1':['1','2']}
res = requests.get(url=url,params=param).json()
print(res)
It works. but when I tried with another way like this,
url = 'https://***.amazonaws.com/default/myAPI?param1=1,2'
res = requests.get(url=url).json()
print(res)
It didn't work with this way.
How to query parameters in case if I want to insert parameter into url directly?
Those tow requests are not equivalent. In order to prove it, we can print the formatted URL for the first request:
url = 'https://***.amazonaws.com/default/myAPI'
param = {'param1':['1','2']}
res = requests.get(url=url,params=param).json()
# Print the request URL
print(res.request.url)
This will print something like:
https://***.amazonaws.com/myAPI?param1=1&param1=2
So, in your second snippet, you probably would want to create your URL as follows:
url = 'https://***.amazonaws.com/myAPI?param1=1&param1=2'
res = requests.get(url=url).json()
print(res)
If you want to separate your parameters with commas, the value for param1 will be a string ('1,2'), not an list.

AWS Athena + boto3: How am I supposed to execute named queries?

Concerning the following draft script I would like to know: How can I execute the named query I created?
I can access the query via the browser interface, but would like to execute it via the Session.
Here the answer is to use the client.start_query_execution(...) command. But whats the point when it is not the named query I created but instead a non-named query with the same query_string. Or am I missing something essential in how to use this?
import boto3
sess = boto3.session.Session(
region_name=region,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY
)
athenaclient = sess.client('athena')
query_string = '''
SELECT *
FROM "ccindex"."ccindex"
WHERE crawl = 'CC-MAIN-2020-34'
AND subset = 'warc'
AND url_host_tld = 'de'
AND url_query IS NULL
AND url_path like '%impressum%'
LIMIT 20000
'''
resp = athenaclient.create_named_query(
Name='filter-ccindex-de',
Description='Filter *.de/impressum websites of Common Crawl index',
Database='ccindex',
QueryString=query_string
)
I don't think there is a direct option to pass named query to your start_query_execution method.But this can be achieved by using get_named_query which accepts Name of the named query and returns QueryString in response.
Then you can parse this response and pass QueryString to start_query_execution method.

OAuth1Session Encoding of URL Django

Alright, I need some assistance with what I think is an easy question, but after digging for quite a while I'll leave it to you brilliant people to show me why I'm not!
I'm attempting to access a provider api, with the URL I'm attempting to access being: .../proposals/AnotherTestProposal/city_furniture/items?filter=description$CONT"Ag" :: this is the way they want it passed, and it should return all items who's descriptions contain the string "Ag". I know I have two of those currently.
I'm using Django 1.9.4, and requests_oauthlib to create an OAuth1Session, I'm doing this successfully also, because I can access resources without URL params. The trouble is I can't get the "?filter=description..." part to encode properly and it's giving me a 401.
When I render the .contents to HTML I get:
{"status": 404, "message": "", "data": [], "arguments": {"id": "items?filter=description$CONT\"Ag\"", "collection": "city_furniture"}}
which is telling telling me that the "AG" part is being escaped, which I don't want. As in: I don't want \"Ag\", I want "Ag".
So, my question: how can I pass the URL, with params, so they are not containing the slashes as these are causing the URL to be invalid and therefore keeping me from accessing the data correctly?
Other, possibly irrelevant info:
The params part of the URL string I'm passing to the OAuth1Session object now is: '/city_furniture/items%3Ffilter%3Ddescription%24CONT%22Ag%22'
An example of filtering from the API's website: proposals/master/roads/items?filter=description$CONT"highway"
I tried passing an 'encoding' arg to .get (in order to change the encoding used by to_native_string) but requests rejected it saying it was an invalid arg
Per comments, some additional code.
Using a function name get_protected_code() to get the OAuth info passed correctly, then in views.py:
api_filter_url = settings.IW_API_MODEL_COLLECTION + '/' + model_id + '/' + settings.IW_API_PROPOSAL_COLLECTION + '/' + proposal_name + '/city_furniture/items%3Ffilter%3Ddescription%24CONT%22Ag%22'
json_model_info_pull = get_protected_data(api_filter_url)
find_vendor = json_model_info_pull.content
def get_protected_data(url_str):
## ...stuffs to create OAuth1Session...
adsk_pull = OAuth1Session(key,
client_secret=secret,
resource_owner_key=oauth_token_use,
resource_owner_secret=oauth_token_secret_use,
)
headers = {'accept': 'application/vnd.autodesk.infraworks-v1+json'}
api_url = settings.ADSK_IW_INFO_BASE_URL + url_str
json_model_info_pull = adsk_pull.get(api_url, headers=headers)
return json_model_info_pull
Looks like you're passing the parameters incorrectly by appending them to the end of the URL in URL-encoding, which requests is respecting as intentional, and the API endpoint is translating in an unintended manner.
From the documentation for requests, you should provide a params keyword argument to requests.get: a dict containing the key-value pairs that should be encoded and sent as the query string for the request. For example, to run a query against the GitHub API, we can pass an API token as a query parameter:
requests.get('http://api.github.com/user',
params={ 'access_token' : MY_OAUTH_TOKEN })
The resultant request will contain a query string with an access_token parameter set to the value stored in MY_OAUTH_TOKEN and escaped properly, as needed. (Such tokens typically contain = characters, for example, that are invalid inside query string values.)

adding parameters to tweepy api request

How does one set the parameters for a request to twitter via tweepy's api.
#https://api.twitter.com/1.1/statuses/user_timeline.json?exclude_replies=true&include_rts=false
import tweepy
#assume tokens and secrets are declared
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
status = api.user_timeline('xxxxxxxxx')
What I get back from this is the "tweets and retweets" from the user inside a collection of Status objects, but
I only want the user's "tweets" returned. After reading the docs, it's still unclear to me on how to modify the request url
I've found success just filtering the json object returned from user_timeline.
This will filter out the user's retweets:
for tweetObj in status:
if hasattr(tweetObj, 'retweeted_status'):
continue
else:
print tweetObj #or whatever else you want to do
But to answer your question, you can pass the optional parameter, include_retweets like so:
status = api.user_timeline('xxxxxxxxx', include_retweets=False)
I like the first method better because the RTs still count against your count & maximum length parameters.

How to get Request property in SOAP UI for REST service test

I created a simple project in SOAP UI, and now am trying to extract some test suit properties using Setup Script. Only thing is, RawRequest and Request properties are empty, but I want to display them. Request does exist, but request and raw request properties are null. This is only case with REST services.
def tcaseList = testSuite.getTestCaseList();
// for each testCase
def countTestCases = tcaseList.size();
for(tc = 0;tc<countTestCases;tc++){
def countTestSteps = tcaseList[tc].getTestStepList().size();
for(i=0;i<countTestSteps;i++)
{// get testStep
def testStep = tcaseList[tc].getTestStepAt(i);
runner = tcaseList[tc].run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
def request = testStep.getPropertyValue("RawRequest");
log.info(request.toString())
Any ideas why this property is null, and how to extract request and display it.
In REST Request on SOAPUI the RawRequest property is only available on REST Request which use POST which sends data not only parameters, in REST Request which use GET the RawRequest is empty. If you want instead to get the value of the GET parameters you can use this in your code:
...
def paramValue = testStep.getPropertyValue("parameterName");
...
In REST Request which use POST and sends data you're already doing correctly with your code:
def request = testStep.getPropertyValue("RawRequest");
I add the follow images to illustrate what I'm explaining:
POST with not empty Request:
GET with empty Request:
UPDATE
So if you want to get all the parameters from your REST Requests you can add the follow groovy snippet to your code:
// your for loop...
for(i=0;i<countTestSteps;i++){// get testStep
def testStep = tcaseList[tc].getTestStepAt(i)
...
// code to get REST Request parameters and log to the console
// to avoid errors if you've testSteps which doesn't are RestTestSteps (i.e groovy...)
if(testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep){
// ? is groovy operator to avoid NPE
def parameters = testStep?.getTestRequest()?.getParams()
// loop through the map parameters
parameters.each { k,v ->
// for each params print the name and the value
log.info "$k : ${v.getValue()}"
}
}
...
If you need more info take a look at RestTestRequestStep API.
Hope this helps,