I have been trying and searched online, but was not able to find a response. Is it possible to achieve the following using Serverless Framework:
I want to use the get.handler that has the code to the following definition for both getting one item and getting all the items. So:
if I hit api.example.com/items/ I retrieve all the items
if I hit api.example.com/items/1234 I retrieve item with id = 1234
- get_items:
handler: project/items/get.handler
events:
- http:
path: items/{itemId}
method: get
So far in the get.handler I check event.pathParameters? event.pathParameters.itemId : null if the specific item exists and call some getItem(itemdId) function and if it does not exits I call a getAll() function.
If I pass the item id in the path it works, but when I make a request for api.example.com/items/ I get the following error:
not a valid key=value pair (missing equal-sign) in Authorization header. This means something is wrong in my path and I have to pass the item id to the path parameters.
My question is: Is there a way I can use multiple paths in the - http: area, or what would be a recommended way to solve this issue (just create two separate handlers) ?
There are two ways to easily accomplish what you're looking for.
Firstly, a lambda function can be triggered by multiple events. You can add another http event to the array of handlers like so:
get_items:
handler: project/items/get.handler
events:
- http:
path: items/{itemId}
method: get
- http:
path: items/
method: get
Alternatively, you could use the {proxy+} argument. You can read more about the various proxy methods here
I've only seen examples with single values in SAM templates:
Environment:
Variables:
TABLE_NAME: my-table
I want to do something like this but doesn't seem to work:
Environment:
Variables:
myVar:
- prop1: aaa
prop2: sdfsdfsd
prop3: ssss
- prop1: bbb
prop2: wwwwww
prop3: aaaaa
I want to have an environment variable that is like a list of objects. I could store a delimited string and parse it myself but I'd prefer to have it be like an object/map/list like if I'm ready a YAML file.
The closest you can do is to json encode the value for your environmental variable
and decode it using the runtime language:
Environment:
Variables:
USER: '{"name": "john", "surname": "galt"}'
If you want to prevent decoding json on each request, move your decoding logic outside the handler, in this case code won't be re-executed while lambda is hot.
Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. We suggest adding logic in your code to check if a connection exists before creating one.
Read about lambda execution model
I personally would create a json file, store it in s3 bucket and use an environment variable to specify s3 url to that file. Additionally, use the same technique I mentioned above or use even more complicated caching mechanism depending on the situation when retrieving the config file
I would like to write the same Ansible template to two different files, one with a value in the file set to True, and the other with a value set to False.
What's the best way to do this? My instinct was to try and pass a value in the template: directive. However, it seems like this is frowned upon.
One way would be to have two different jinja files with almost entirely the same contents; one has the value set to True and the other False.
Another way would be to define a variable, write one template, then use set_fact to change the variable's value, then write the second file. This also seems a little cumbersome.
Another would be to have the template detect what filename it's being rendered as, somehow? And branch in the template based on that.
I must be missing something obvious.
With Ansible 2.x you can use vars: with tasks:
---
- hosts: localhost
tasks:
- template: src=my_template.j2 dest=out1.txt
vars:
name: John
- template: src=my_template.j2 dest=out2.txt
vars:
name: Jane
I just stumbled on to Ember Data Polymorphic Relationships. I think they would be useful for what I am working on. Here is my main model. This model defines a has many relationship with topics thats both async and polymorphic.
App.Source = DS.Model.extend({
name: DS.attr('string'),
type: DS.attr('string'),
locationType: DS.attr('string'),
locationSpecific: DS.attr('boolean'),
primary: DS.attr('boolean'),
published: DS.attr('boolean'),
sort: DS.attr('number'),
topics: DS.hasMany('topic', {
async: true,
polymorphic: true
})
});
Next we have the Topics with a Base class being 'Topic'
App.Topic = DS.Model.extend({
name: DS.attr('string'),
sort: DS.attr('number'),
source: DS.belongsTo('source')
});
App.RegTopic = App.Topic.extend({
test: DS.attr('number', {
defaultValue: 8
}),
notes: DS.hasMany('notes', {
async: true
})
});
App.SummaryTopic = App.Topic.extend({
number: DS.attr('number', {
defaultValue: 9
})
});
and here is how I call to get the topics
App.TopicsRoute = Ember.Route.extend({
model: function() {
return this.modelFor('source').get('topics');
}
});
When I list the sources, I get a list of the following objects back
{
id: 1
name: "test"
type: "testType"
locationType: "international"
locationSpecific: "true"
primary: true
published: true
sort: 1
links: {
topics: "/topics?sourceId=1"
}
}
then my topic call gets objects like these back
{
id: 4
sourceId: 1
name: Topic 4
type: "regTopic"
sort: 1
}
Am I missing something? Can you not use polymorphic relationships with the 'links' object?
From my understanding of the Polymorphic Relationship, when I make the call to /topics?sourceId=1 its should be essentially loading 2 different topics in one call so then I can display the regTopic and summaryTopic on the same page but in seperate lists and keep them as seperate objects?
Here is an updated jsbin, that seems close to working. http://emberjs.jsbin.com/medojitibo/1/edit?html,js,console,output
The issue I am seeing it that in my controller, the topics are all App.Topic in the list. There is no App.RegTopic or App.SummaryTopic
The links element looks like JSON-API, for which there is an addon. I have had to make modifications for polymorphism in my fork. It may be easier to modify your back-end to support the API specified by Ember Data.
I would not recommend using Ember Data polymorphism unless you really know what you are doing and have convinced yourself it does what you need. Currently it's designed for fairly limited use cases. There are a number of discussions and proposals on the topic that you can track down if you are interested.
You apparently believe, or the API believes, that the format for the links property in the JSON for the source object is an object containing a single (?) topics property which gives the API endpoint for retrieving a polymorphic topic. That's not how it works at all.
The correct syntax for the links property in the returned JSON would be an array of {id, type} tuplets:
{
id: 1
name: "test"
type: "testType"
...
links: [
{ id: 'topic1', type: 'regTopic'},
{ id: 'topic2', type: 'summaryTopic' }
]
}
The format of the data for the association, as shown above, is an array of id/type objects, rather than just an array of ids. This is required for Ember Data polymorphism to work. If you can't arrange for your back-end to produce exactly this kind of format, you will end up patching and hacking a number of methods in adapters and serializers and at the end of the day building your own version of polymorphism, probably not an endeavor you wish to embark on.
Without this special form of JSON for the association properties, Ember Data has no idea of what type of object to go looking for. With it, Ember Data will create the proper instance type of topic for each entry in this topics array, and essentially make a call to find('reg-topic', 'topic1') and find('summary-topic', 'topic2') to get its data. That means that you will also need a separate API endpoint for each subtype of topic. (Matters differ if the topics are embedded, an alternative I won't go into here.)
So far so good, perhaps. But this starts to break down quickly once you start to do more sophisticated things. Let's say you want to fetch/retrieve a topic with a particular ID. One might think that since "topics are polymorphic", you could just do this.store.get('topic', id). But you can't. The store holds RegTopics and SummaryTopics in separate places, and does not connect them with Topic. If you ask for a 'Topic', it does not know to look under different (sub-)models for instances already in the store. So you have to know in advance what the type is and ask the store for the record of that particular type, which sort of defeats the whole purpose of polymorphism. If you simply call the topics endpoint, Ember Data will create a new model instance for Topic--it has no idea that it is supposed to examine the type field of the returned JSON to govern what subtype to turn it into.
The point is that here topics are not polymorphic. In Ember Data polymorphism is not at the model level; it's at the association level. What is polymorphic here, in other words, is not the topic model itself; it's the associations held in the links property on the source model. In other words, Ember Data has no idea about a model itself being polymorphic; it just knows that particular association (hasMany or belongsTo) can hold polymorphic values. For some insights into what might be required to make Ember Data truly support polymorphic models, albeit in a slightly different context, take a look at https://github.com/Bestra/ember-data-sti-guide.
To answer your question in your comment, no, Ember Data polymorphism is not the best way to do what you want. Do not venture out into these uncharted waters. I'd adopt a strategy of "poor man's polymorphism", where your Topic model has a type field, and might or might have some different properties depending on the type. You can then sort and group and filter on type to your heart's content.
Usually to retrieve a resource one uses:
GET http://ws.mydomain.com/resource/123212
But what if your item IDs are HTTP URIs?:
GET http://ws.mydomain.com/resource/http://id.someotherdomain.com/SGX.3211
Browsers replace two slashes with one, and the request turns into:
GET http://ws.mydomain.com/resource/http:/id.someotherdomain.com/SGX.3211
which will not work.
URI encoding the "http://id.someotherdomain.com/SGX.3211" -part results in HTTP 400 - Bad request.
Is there a best practice for handling this?
Edit:
Then of course if we would need to have (I don't at the moment) request in form:
resources/ID/collections/ID
and all IDs are HTTP URIs, things get out of hand... Possibly one could do something like this and parse the contents inside the curly braces:
resources/{http://id...}/collections/{http://id...}
Encode the other system's URI, and then pass the value as a query parameter:
GET http://ws.mydomain.com/resource?ref=http%3A%2F%2Fid.someotherdomain.com%2FSGX.3211
Ugly looking, but no one said that URIs used in a REST architecture have to be beautiful. :)
By the way, a GET actually looks like this when it's sent:
GET /resource?ref=http%3A%2F%2Fid.someotherdomain.com%2FSGX.3211 HTTP/1.1
Host: ws.mydomain.com
UPDATE: apparently you no longer have to encode "/" and "?" within a query component. From RFC 3986:
The characters slash ("/") and question mark ("?") may represent data
within the query component. Beware that some older, erroneous
implementations may not handle such data correctly when it is used as
the base URI for relative references (Section 5.1), apparently
because they fail to distinguish query data from path data when
looking for hierarchical separators. However, as query components are
often used to carry identifying information in the form of "key=value"
pairs and one frequently used value is a reference to another URI, it
is sometimes better for usability to avoid percent-encoding those
characters.
So you could legally do this:
GET /resource?ref=id.someotherdomain.com/SGX.3211 HTTP/1.1
Host: ws.mydomain.com