one LAMBDA function work with few methods - aws-lambda

I have an API that has different methods - (GET, POST, DELETE) Is there a way to make one LAMBDA function work with all of these methods or does each method have its own LAMBDA function?
I mean I have parts in the code that each part is supposed to work in calling a different method.
and what if i have a few resources?
client = boto3.resource('dynamodb')
table = client.Table('papi.table')
def lambda_handler(event, context):
//for GET METHOD
tab= table.scan()
item = tab['Items']
for id in item:
print(id)
// for POST
response = table.put_item(
Item={
'id':"1"
}
)
return response
and so..

You can get the httpMethod parameter from the event, see Documentation for the API gateway response to the Lambda.

Related

Invoking an AWS Lambda function from another Lambda with multiple method of a service without any conditional parameter

I am familiar with invoking lambda from another lambda and also to pass param to handler function based on which it calls different methods from a service(Same as stated here AWS Lambda - Invoke a method of another lambda function), But when there is a case to expose all the methods of a service its quite tedious to add multiple condition in the handler function.
So my question is Is there any dynamic way we can expose all the methods of the service using single lambda handler function, or may be any other efficient way. My main motive is to expose whole method in a service using lambda handler so that I don't have to add different conditions and another lambda can invoke it method name without any conditional parameter.
Sample code here
export const handler = async function(params: params,
context: Context,
callback: Callback) {
if(params.type === 'type1'){
const results = await testService.testmethod1(params.value);
context.succeed(results);
}
else if(params.type === 'type2'){
const results = await testService.testmethod2(params.value);
context.succeed(results);
} and so on...
};
So Is there any way we can expose all methods of testService(above we have exposed to methods testmethod1 and testmethode, there can be other methods as well) without all these conditional statements?

Can you access the req object in the cy.route method before returning the stubbed data?

I am looking to get the req object in the cypress route method. By doing so I could decide what the user gets back when hitting the graphql route which has dynamic responses. Has anyone be able to accomplished this?
I think having access to this would be incredibly useful.
I hope this helps, where xhr.requestBody does help in accessing the request body,
cy.route("GET", "/login").as("getLogin");
cy.get("#contactID").type("email#gmail.com");
cy.contains("Login").click();
cy.wait("#getLogin").then(function(xhr) {
// we can now access the low level xhr
// that contains the request body,
// response body, status, etc
const request = xhr.requestBody;
expect(response[0]).to.have.property("SomeKey", "Data");
const response = xhr.responseBody;
expect(response[0]).to.have.property("LineName", "Line A");
});

angular $http service singleton clash

$http service in angular is singleton, and my previous experience with xhr/ ajax request shown to have clash when two request share the same xhr object. Should it not a problem with angular ? if yes how angular handles such situation ?
I think you're misunderstanding the fact that the $http service is a singleton to mean that all requests somehow will share the same XHR object. They don't.
The $http service itself is a singleton, but that doesn't mean that the requests share the same XHR object.
Anytime you call an $http service method (for example, $http#get), it initializes a new asynchronous request... However, it doesn't initialize a new $http object.
Take a look at some of Addy Osmani's sample code for the singleton pattern:
return {
getInstance: function () {
if ( !instance ) {
instance = init();
}
return instance;
}
};
The singleton pattern simply ensures that a new instance of the $http service itself doesn't get initiliazed over and over again... But it doesn't mean there is just one XHR object.
The pseudo-code for the $http service would look something like this:
var $httpProvider = (function() {
var somePrivateConfig = "something important";
var service = {
request: function() {
// make an HTTP request with an XHR object
}
};
return {
init: function() {
// this is the important part that makes sure its a singleton
window.$http = window.$http || service;
}
};
})();
/**
* Something like this would be called whenever you
* inject the $http service as a dependency...
* However, since it could be passed into multiple things all in the same runtime,
* like controllers, directives, etc., we only need to initialize it ONE time
*/
$httpProvider.init();
/**
* Now let's pretend we're inside, say, a controller.
*
* This method can safely be called many times,
* but the method is always being called from the same singleton object
*/
$http.request();
Also, you'll notice that there's a local variable somePrivateConfig within the $httpProvider IIFE. If we were to re-initialize a new $http every time its injected to a component (whether that's a controller, directive, or whatever), a new private variable would be created, but we always want to be referencing that same value throughout the lifecycle of the $http object, so that we can guarantee that all those components are always referencing the same information.
But this has nothing to do with the XHR object itself. I may have misused some of the terminology above and misrepresented where and how providers themselves within the context of AngularJS are initialized into singleton objects, but the principle of the singleton pattern still stands in that it simply means the async request wrapper "class" (which is the $http service) is a singleton, but the XHR isn't.
$http requests are async and return a promise with methods success() and error(). Below more information ($q service, which is an implementation of promises by Angularjs):
"A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing"
Read here:
https://docs.angularjs.org/api/ng/service/$http
https://docs.angularjs.org/api/ng/service/$q

WebApi Put, how pass the params?

I have created a webApi that automatically created som methods for me.
That Put-Method got created like this:
// PUT: api/Actor/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutActor(int id, Actor actor)
{
//Code that updates the actor
}
But How can i call this method?
Im thinking that it must start with:
HttpResponseMessage response = client.PutAsJsonAsync("api/Actors/") <-How can i add the two params?
According to some posts you cant do it without workarounds:
WebAPI Multiple Put/Post parameters
But it seems strange considering that the method got created to me automatically. Should be a standard way to do it no?
You could pass the id in the route, whereas the Actor in the payload using the following overload which takes as second argument an object:
HttpResponseMessage response = client.PutAsJsonAsync("api/Actors/123", new
{
actor = new Actor
{
Name = "John Smith",
Age = 30,
}
});

Making the body of a function use a mock of an object

I have the following method in a controller:
def webhook
data_json = JSON.parse(request.body.read) # it comes from the test, it's OK
event = Stripe::Event.retrieve(data_json[:id]) # it's not OK, it's a real request to Stripe
stripe_cust_id = event.data.object.customer
user = User.where(stripe_customer_id: stripe_cust_id)
#.....
In a spec file I create a mock for event and then make a post request to webhook in a test. I'm not allowed to change the body or signature of webhook because I'm testing it. So how do I make it use the mock I create?
describe '#webhook' do
it 'something' do
user = FactoryGirl.create(:user)
event = StripeMock.mock_webhook_event('invoice.payment_succeeded')
post(:webhook, event.to_json)
#error because webhook makes a real request to Stripe
mock(Stripe::Event).retrieve(any) do
event
end
That should return event from any call to retrieve on Stripe::Event. Works with rr.
If your concern is only about external requests, you can try something like Vcr or WebMock to mock the responce.

Resources