Swagger UI with digest authentication - javascript

I'm using Swagger to produce the documentation of my REST API. According to what is written around, the Swagger UI does not offer support for basic authentication (differently from the online editor). My problem is that one of my APIs is a POST that require digest (not even basic) authentication.
A possible solution I found around is to add a fixed user:pass authentication header in the request via javascript code. This should be easily done according to the Swagger UI documentation (see Custom Header Parameters). I report the incriminated code line:
swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "XXXX", "header"));
Unfortunately it doesn't work. The swaggerUi.api field results uninitialised (null) after I initialise the SwaggerUi object, and as a consequence swaggerUi.api.clientAuthorizationsis undefined. I tried initialising such fields in different way, failing every time. I tried also similar calls to the API I found in threads discussing this topic, but none of them has worked. Does anyone have an idea about that? The documentation is not particularly clear about that.
For completeness, I report the js snippet where I initialise the Swagger UI
var urlPush = "./doc_push.yaml";
window.swaggerUiPush = new SwaggerUi({
url: urlPush,
dom_id: "swagger-ui-container-push",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onFailure: function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion: "list",
jsonEditor: false,
defaultModelRendering: 'model',
showRequestHeaders: false,
});

Try using SwaggerClient.PasswordAuthorization instead of SwaggerClient.ApiKeyAuthorization -
var username = $('#input_username').val();
var password = $('#input_password').val();
var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
window.swaggerUi.api.clientAuthorizations.add("basicAuth", basicAuth);

Related

How do I retrieve a list of posts based on a tag slug in WP Rest API V2?

I have an application that retrieves data from a Wordpress site using the WP Rest API V2. The API has recently been change from using a 'filter[tag]=' to a new syntax. I am looking to retrieve a list of posts within a specific tag.
Old syntax:
http://demo.wp-api.org/wp-json/wp/v2/posts?filter[tag]=slug
The new syntax looks like this:
http://demo.wp-api.org/wp-json/wp/v2/posts?tags=id
It now takes an ID integer instead of a slug as argument. I have looked over the documentation but I can't seem to find a solution for this. I have no option of using the id, I can only use the slug!
The WP Rest API documentation can be found here: https://developer.wordpress.org/rest-api/reference/posts/
Do you have any idea how to solve this?
Just get the list of tags by calling http://demo.wp-api.org/wp-json/wp/v2/tags before your requesting posts on a specifiy tagId. In that way you will find the tagId you need by filtering the data by your "slug". The demo API is not support CORS thats way I dont create a working fiddle.
Nested $http request bad example
$http({
method: 'GET',
url: 'http://demo.wp-api.org/wp-json/wp/v2/tags'
}).then(function (result) {
var filtered = filterFilter(result.data, { slug: 'dolor'});
$http({
method: 'GET',
url: 'http://demo.wp-api.org/wp-json/wp/v2/posts?tags=' + filtered[0].id
}).then(function (result) {
console.log(result.data);
});
});
Looking at the official API docs for TAGS, you can search for strings in the tags schema with a request like this:
http://demo.wp-api.org/wp-json/wp/v2/tags?search=yourslug
The resulting JSON output will display tags containing the string you searched in any of the schema fields, like slug.
After you retrieved the desired tag, you can fetch the posts with that specific tag using it's ID (as suggested by #lin).

How to get Swagger API JS function names?

I'm new to Wordnik and Swagger in general. I'm interacting with it through the Node.js JS module.
When looking at the generated Swagger UI pages, such as the sample Petstore Swagger one:
it is difficult to tell what the JavaScript functions should be when querying the server.
By JavaScript functions, I mean similar to the Petstore sample example (from the swagger-js docs):
var Swagger = require('swagger-client');
var client = new Swagger({
url: 'http://petstore.swagger.io/v2/swagger.json',
success: function() {
client.pet.getPetById({petId:7},{responseContentType: 'application/json'},function(pet){
console.log('pet', pet);
});
}
});
After connecting to swagger.json, how do they know the function to query the getPetById() function when the docs only show GET /pet/{petId}?
When using the Wordnik API, I've found it a general rule of thumb to use the get{DATATYPE}() function (with {DATATYPE} replaced with an appropriate value, of course), but the pattern has broken with getPronunciation() — it doesn't work. I don't believe the docs say it anywhere.
How could I find the JS functions for Swagger APIs?
The JavaScript method name is the same as the nickname and/or operationId field of each operation in the Swagger document, depending on which one is available.
This is an example from the Wordnik:
"path": "/word.{format}/{word}/pronunciations",
"description": "",
"operations": [
{
...
"nickname": "getTextPronunciations",
"responseClass": "List[TextPron]"
}
]
},
In this example getTextPronunciations is the JS method name.
In Wordnik, you can get the swagger.json by clicking on the Raw button on the word section of the document (next to the Expand Operations button). You can find all the other swagger.json files here: http://developer.wordnik.com/v4/

With VersionOne api, how to implement fetch of Story Names when implemented by javascript / html? query.v1 using Oauth2

I would like to begin consuming VersionOne api to use as building block for presenting custom HTML page view of Story Names (expand details on this later once I got the initial thing working). I'd like to formulate this in HTML, javascript, JSON, OAUTH2 (no callback URL) implementation. For this implementation the query.v1 endpoint is required and the usage/setup instructions for me aren't as clear/easy to use as the rest-1.v1.
I did "build"/download my personal client secret("v1_client_secrets.json). Although I do not know what "using the scope query-api-1.0" from the documentation means or how it would get applied. Api documentation I have been reading is here: https://community.versionone.com/Developers/Developer-Library/Sample_Code/Tour_of_query.v1
I am fine with the json data GET pattern below.
{
"from": "Story",
"select": [
"Name"
]
}
Although there are examples of the JSON patterns, I can't seem to understand how to code it from start to finish. Is there a starter template script (includes all html/javascript) based on my details, that someone could pass along? I am assuming jquery, angular, ajax would be incorporated in the retrieval/parse/read process.
FYI, I am new to this coding, especially consuming api, so hoping this all makes sense.
Using the Javascript SDK you can communicate with a VersionOne instance. If you are using node you can install the package with npm install v1sdk. Otherwise the source code can be downloaded at https://github.com/versionone/VersionOne.SDK.JavaScript/ . An example of this.
import $ from 'jquery';
import sdk, {jqueryConnector} from 'v1sdk';
const jqueryConnectedSdk = jqueryConnector($)(sdk);
const v1 = jqueryConnectedSdk('www14.v1host.com', 'v1sdktesting', 443, true)
.withCreds('admin', 'admin'); // usage with username/password
// .withAccessToken('your token'); // usage with access tokens
v1.create('Story', {estimate: 5, status: 'Not Started'})
.then((story) => v1.update(story.oidToken, {estimate: 7}))
.then(v1.query({
from: 'Story',
select: ['Estimate', 'Status'],
where: {
Status: 'Not Started'
}
}))
.then(console.log)
.catch(console.log);
This example shows creating a v1 connection, creating a story, updating that story, and then querying for that same story and selecting the Estimate and Status where the Status is 'Not Started'. This is an article that gives an example of displaying the backlog using the SDK http://walkerrandolphsmith.com/blog/v1sdk/. It also has an accompanying git repo you can look at mentioned in the article.

how to create autocomplete from google.maps.places.autocomplete in ExtJS

Please correct me if I'm wrong...
I write a code with no errors, in before edit this post.
but after i read a comment about how to create a Minimal, Complete, and Verifiable example. I'm very confuse, because my minimal script before didn't work, i cann't include complete code, because my complex code, and I don't have verifiable example.
I just need to know how to create autocomplete from google.maps.places to autocomplete my textfield in EXTJS.
Thanks before..
I've solved this question from #Rob Schmuecker answers.
So, the focus of this question about using an appropriate proxy to call the json from google like using :
addressModel.getProxy().setExtraParam('url', 'http://maps.google.com/maps/api/geocode/json?address=' + queryString + '&sensor=false');
to set the params dynamically, from this data model :
addressModel = Ext.define("Addresses", {
extend: 'Ext.data.Model',
proxy: {
type: 'jsonp',
url: 'https://jsonp.nodejitsu.com',
reader: {
type: 'json',
root: 'results',
totalProperty: 'totalCount'
}
},
fields: ['formatted_address']
});
after that you can create a combobox and add listener to the combobox each keyup to call the extraparam in your proxy.
this is the demo from #Rob Schmuecker :
https://fiddle.sencha.com/#fiddle/g70
and this is for the complete answer from Rob, who solved my problem :
Check This...
I've figured out a way to solve this using the Google Places-Library.
My Solution using Ext JS 3.4
1) Integrate the Google Places API
2) Create a custom Proxy (I've had extended the Ext.data.DataProxy)
2a) Set the proxy api (read: true)
2b) override the doRequest method. there you can make the calls to i.e. google.maps.PlacesService.textSearch
2c) Handle the Places Request using a Reader (I've created my custom reader)
3) store your data in a store (I've created my custom store)
4) create component that consumes data from the store
Be aware that your application has to comply to the Google Maps/Google Earth APIs Terms of Service! (You Have to show the logos, third party right holders, ect.)
There you have it!

Reddit Api Error trying to get reddit self text via snoocore node.js

I'm tryng to get the self.text on a post and using this route:
reddit('/r/Denmark/comments/2jc5yk/how_to_live_in_denmark.json').listing({
context: 1,
limit: 10,
sort: 'hot',
})
.then(function(result) {
console.log(result);
});
I have also tried using .get(), without .json and without /how_to_live_in_denmark but still the same error.
When I input the route in my browser, I get the desired JSON.
The error i get:
Uncaught Error: Invalid path provided! This endpoint does not exist. Make sure that your call matches the routes that are defined in Reddit's API documentation
What am i doing wrong?
Update: 2015-02-09
Snoocore now accepts URLS's with embedded values and does not require placeholders if you do not wish to use them.
I'm the creator of this API wrapper. I'll have to monitor StackOverflow a little bit more to catch these quicker. Feel free to open new issues on GitHub as well when you get stuck on something for a quicker response!
It looks like you are trying to call this endpoint:
GET /r/[subreddit]/comments/article
Basically anything that is in brackets is optional in Snoocore, and anything in italics is an URL parameter that you will need to define placeholders for in the call (using $parameter). More information on this can be read in the documentation (feel free to ask questions or improve upon the documentation if it isn't clear!)
So in your case, you will want to do this:
reddit('/r/$subreddit/comments/$article').get({
$subreddit: 'Denmark',
$article: '2jc5yk',
context: 1,
limit: 10,
sort: 'hot'
}).done(function(result) {
console.log(result);
});
Note that instead of defining the url parameters in the call, the are now referenced by $subreddit and $article respectivly.
Note that comments are not a listing, and therefore can't use the listings interface as you tried to do in your question.

Categories

Resources