Play reverse routing - getting absolute url - playframework-2.2

How can I get the absolute URL in play 2.2 scala when doing the following:
val promoLink = routes.Promotions.promotionsCategory(DOCID, slug)
//routes file
GET /promotions/:DOCID:/slug controllers.Promotions.promoCat(DOCID, slug)
As it stands I get a "found : play.api.mvc.Call" type mismatch on expecting a string
thanks

I suppose your promoLink should be a String containing an URL? Your question sounds a bit unclear.
If so then you probably need this:
val promoLink = routes.Promotions.promotionsCategory(DOCID, slug).absoluteURL(false)(request)
false in the .absoluteURL(false) stands for the isSecure parameter which will give you http or https url.
If you have an implicit request in scope you may omit the last (request) part

Related

Is there a way to load externaldata from a variable URL in Kusto?

With Kusto's externaldata operator, I am able to load a text file from my website:
let appUsers = externaldata(userId:string)['https://www.mybusiness.com/files/salesforce-guid.txt'];
However I need to manipulate the URL string based on differing factors. When I try moving the URL into a variable, Kusto gives me syntax errors ( expected ']' )
let url = 'https://www.mybusiness.com/files/salesforce-guid.txt';
let appUsers = externaldata(userId:string)[url]; // syntax err
Is there a way for Kusto to load externaldata from a variable URL?
Such functionality doesn't exist.
You can suggest it on https://aka.ms/adx.uservoice

How do I get the correct path to a JSON file from a URL?

How do I get the "request url" part of the get request?
The number part is time in milliseconds but the part before the ".dat" in the URL changes for every game so I need a way to get the whole URL, using requests and BeautifulSoup4.
link to page https://www.oddsportal.com/soccer/germany/bundesliga/1-fc-koln-holstein-kiel-0IRBLw8b/
This was an interesting challenge so I decided to have a look.
You can construct the url from various parts of the initial response, with the inclusion of a tab mapping for football (shown in dictionary). It may be possibly to derive the mappings for the dictionary dynamically from the onmousedown arguments and the associated uid function. I started looking into it and may carry on time permits. Hardcoding for football, for full/1st half/2nd half tabs, seems to be ok for now.
import requests
import re, urllib, time
time_lkup = {
'full_time':'1-2',
'first_half':'1-3',
'second_half':'1-4'
}
with requests.Session() as s:
s.headers = {'User-Agent':'Mozilla/5.0',
'referer': 'https://www.oddsportal.com'}
r = s.get('https://www.oddsportal.com/soccer/germany/bundesliga/1-fc-koln-holstein-kiel-0IRBLw8b/')
version_id = re.search(r'"versionId":(\d+)', r.text).group(1)
sport_id = re.search(r'"sportId":(\d+)', r.text).group(1)
xeid = re.search(r'"id":"(.*?)"', r.text).group(1)
xhash = urllib.parse.unquote(re.search(r'"xhash":"(.*?)"', r.text).group(1))
unix = int(time.time())
url = f'https://fb.oddsportal.com/feed/match/{version_id}-{sport_id}-{xeid}-{time_lkup["full_time"]}-{xhash}.dat?_={unix}'
print(url)
r = s.get(url)
print(r.text)

Can Karate generate multiple query parameters with the same name?

I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:
http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC
Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:
Given path 'v1/orders'
And param sort = 'order.orderNumber'
And param sort = 'order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/orders'
And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC
The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file
It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.
So, is there a way in Karate to set multiple URL query parameters with the same name?
Yes you can generate multiple query parameters with the same name in karate
All values of similar key should be provided in an array.
Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200
And for setting single parameter using param it will be like
And param sort = ["order.orderNumber","order.customer.name,DESC"]

Adding a query to a URL that already has a question mark with Retrofit

By good design, Retrofit will add your query to your URL as an ampersand rather than a question mark if there's already a question mark in the URL. However, I have no control over the URL and so am unable to change it. Is there a way that I can override #Query so that it will use a ? instead?
interface Service {
#GET("/php/project/?sign.externalGetToken")
fun getAuthToken(
#Query("site") site: String,
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
}
Printed URL:
http://baseurl.com/php/project/?sign.get&site=my_site&time=1538755984978&secretkey=site_secret_key
I need that URL to be:
http://baseurl.com/php/project/?sign.get?site=my_site&time=1538755984978&secretkey=site_secret_key
Note the double ? in ?sign.get?site.
Is an url like this acceptable ?
http://baseurl.com/php/project/?sign.get?&site=my_site&time=1538755984978&secretkey=site_secret_key
There is an & just after the second question mark, but in theory the server should still be able to read it. If yes you can do it simply like this:
#GET("/php/project/?sign.externalGetToken?")
Or you can also try like this:
#GET("/php/project/?sign.externalGetToken")
fun getAuthToken(
#Query(value = "?site", encoded = true) site: String, // encoded ensure that retrofit doesn't encode the ? into '%3F'
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
which will result in
http://baseurl.com/php/project/?sign.get&?site=my_site&time=1538755984978&secretkey=site_secret_key
the & will this time be before the question mark, but not sure if it will work. Also this second solution requires you never pass null for the site parameter of your call, else the "?site" will simply not be printed.
As a last resort, if nothing of the above works, you can try to use instead the #Url annotation from retrofit. It seems a little in contradiction with the use of a service interface if you ask me, but at least it should work.
Just replace your declaration by this:
#GET
fun getAuthToken(
#Url url: String,
#Query("time") time: String,
#Query("secretkey") secretKey: String
): Call<TokenResponse>
then just call it like this
service.getAuthToken("/php/project/?sign.externalGetToken?site="+ yourSite, yourTimeString, yourSecretKey");
and you will have the url you want. But it requires to always send the url as a parameter.

m.request: use URL which contains colons

I have a m.request call like this:
mCmdName = "cn:cmd:list:deselectAll";
m.request({
method : "POST",
url : "/testing/cmd/" + mCmdName,
data: data
});
Now m.request calls
xhrOptions.url = parameterizeUrl(xhrOptions.url, xhrOptions.data);
and tries to replace all ':[name]' parts with data[name] which results in 'undefined' as data doesn't contain any of the keys. Data is just the data object of the XHR request.
Is there a way to prohibit this default behavior?
Thanks, Stefan
PS: I'm asking here and not in mithril mailing list because I can't post there for incomprehensible reasons. Maybe somebody can give me a hint on this.
Have you tried
encodeURIComponent("cn:cmd:list:deselectAll")
which gives you
cn%3Acmd%3Alist%3AdeselectAll
If necessary you can decode on the server.