Skip to content

Commit

Permalink
Change AWS sdk requests to http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
trikehor committed May 12, 2022
1 parent 0004177 commit ec2b8bd
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 92 deletions.
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MatDialogModule } from '@angular/material/dialog';
import { MatSelectModule } from '@angular/material/select';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import {ScrollingModule} from '@angular/cdk/scrolling';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
declarations: [
Expand All @@ -33,6 +34,7 @@ import {ScrollingModule} from '@angular/cdk/scrolling';
],
imports: [
BrowserModule,
HttpClientModule,
FormsModule,
AppRoutingModule,
BrowserAnimationsModule,
Expand Down
31 changes: 10 additions & 21 deletions src/app/components/settings/settings.component.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
<h2>AWS Region</h2>
<h2>Get Todos Url</h2>
<mat-form-field class="full-width" appearance="fill">
<mat-label>aws_region</mat-label>
<input matInput [(ngModel)]="awsConfig.region">
<mat-label>Get Todos Url</mat-label>
<input matInput [(ngModel)]="awsConfig.getTodosUrl">
</mat-form-field>
<h2>AWS Access Key Id</h2>
<h2>Update Todos Url</h2>
<mat-form-field class="full-width" appearance="fill">
<mat-label>aws_access_key_id</mat-label>
<input matInput [(ngModel)]="awsConfig.accessKeyId">
<mat-label>Update Todos Url</mat-label>
<input matInput [(ngModel)]="awsConfig.updateTodosUrl">
</mat-form-field>
<h2>AWS Secret Access Key</h2>
<h2>Delete Todos Url</h2>
<mat-form-field class="full-width" appearance="fill">
<mat-label>aws_secret_access_key</mat-label>
<input matInput [(ngModel)]="awsConfig.secretAccessKey">
</mat-form-field>
<h2>AWS Session Token</h2>
<mat-form-field class="full-width" appearance="fill">
<mat-label>aws_session_token</mat-label>
<textarea matInput [(ngModel)]="awsConfig.sessionToken">
</textarea>
<mat-label>Delete Todos Url</mat-label>
<input matInput [(ngModel)]="awsConfig.deleteTodosUrl">
</mat-form-field>
<h2>Save in Local Storage</h2>
<p>
<button mat-raised-button color="primary" (click)="saveConfiguration()">
Save configuration
</button>
</p>
<h2>Test Configuration</h2>
<p>
<button mat-raised-button color="primary" (click)="runConfigurationTest()">
Run Configuration Test
</button>
</p>
10 changes: 0 additions & 10 deletions src/app/components/settings/settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ export class SettingsComponent implements OnInit {
this.awsConfig = this.dataService.awsConfig;
}

async runConfigurationTest() {
try {
await this.dataService.healthcheck();
this.showSnackBar("Connection to AWS Services works!", "success");
} catch (err) {
console.error(err);
this.showSnackBar("Connection to AWS Services failed! Take a look at the error message for more details...", "error");
}
}

showSnackBar(message: string, panelClass: string = "info") {
this.snackBar.open(message, undefined, { duration: 5000, panelClass: [panelClass], verticalPosition: "bottom" });
}
Expand Down
7 changes: 3 additions & 4 deletions src/app/interfaces/aws-config/aws-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface AwsConfig {
region: string;
accessKeyId: string;
secretAccessKey: string;
sessionToken: string;
getTodosUrl: string;
updateTodosUrl: string;
deleteTodosUrl: string;
}
86 changes: 29 additions & 57 deletions src/app/services/data/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { Injectable } from '@angular/core';
import * as AWS from 'aws-sdk';
import { AwsConfig } from 'src/app/interfaces/aws-config/aws-config';
import { TodoItem } from 'src/app/interfaces/todo-item/todo-item';

export interface AwsLambdaParams {
FunctionName: string,
InvocationType: string,
LogType: string,
Payload?: string
}
import { HttpClient, HttpErrorResponse } from '@angular/common/http';

@Injectable({
providedIn: 'root'
Expand All @@ -17,13 +11,12 @@ export interface AwsLambdaParams {
export class DataService {
lambda!: AWS.Lambda;
awsConfig: AwsConfig = {
region: "",
accessKeyId: "",
secretAccessKey: "",
sessionToken: ""
getTodosUrl: "",
updateTodosUrl: "",
deleteTodosUrl: ""
};

constructor() {
constructor(private http: HttpClient) {
let awsConfig = localStorage.getItem("awsConfig");

if (awsConfig) {
Expand All @@ -39,66 +32,45 @@ export class DataService {
}

getTodoItems (): Promise<any> {
let params : AwsLambdaParams = {
InvocationType: "RequestResponse",
LogType: "None",
FunctionName: "arn:aws:lambda:us-east-1:344646043511:function:getTodoItems"
}

let response: Promise<any> = this.callLambda(params);
let response: Promise<any> = this.callLambdaGet(this.awsConfig.getTodosUrl);
return response;
}

updateTodoItem (item: TodoItem): Promise<any> {
let params : AwsLambdaParams = {
InvocationType: "RequestResponse",
LogType: "None",
FunctionName: "arn:aws:lambda:us-east-1:344646043511:function:updateItem",
Payload: JSON.stringify(item)
}

let response: Promise<any> = this.callLambda(params);
let requestBody = JSON.stringify(item)
let response: Promise<any> = this.callLambdaPost(this.awsConfig.updateTodosUrl, requestBody);
return response;
}

deleteTodoItem (item: TodoItem): Promise<any> {
let params : AwsLambdaParams = {
InvocationType: "RequestResponse",
LogType: "None",
FunctionName: "arn:aws:lambda:us-east-1:344646043511:function:deleteItem",
Payload: JSON.stringify(item)
}

let response: Promise<any> = this.callLambda(params);
let requestBody = JSON.stringify(item)
let response: Promise<any> = this.callLambdaPost(this.awsConfig.deleteTodosUrl, requestBody);
return response;
}

callLambda(params: AwsLambdaParams): Promise<any> {
callLambdaGet(url: string): Promise<any> {
const headers = { "Access-Control-Allow-Origin": "*" };

return new Promise((resolve, reject) => {
this.lambda.invoke(params, function(err, data) {
if (err) {
reject(err);
}
else {
let payload = JSON.parse(<string>data.Payload)
resolve(payload);
}
});
this.http
.get(url, { headers })
.subscribe(
data => resolve(data),
error => reject(error)
)
})
}

healthcheck() {
let params = {
FunctionName: 'arn:aws:lambda:us-east-1:344646043511:function:HelloWorld',
InvocationType: "RequestResponse",
LogType: "None",
Payload: JSON.stringify({
"key1": "value1",
"key2": "value2",
"key3": "value3"
}),
};
callLambdaPost(url: string, requestBody: any): Promise<any> {
const headers = { "Access-Control-Allow-Origin": "*" };

return this.callLambda(params);
return new Promise((resolve, reject) => {
this.http
.post<any>(url, requestBody, { headers })
.subscribe(
data => resolve(data),
error => reject(error)
)
})
}
}
Empty file.
1 change: 1 addition & 0 deletions src/app/todo-list/todo-list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>todo-list works!</p>
25 changes: 25 additions & 0 deletions src/app/todo-list/todo-list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TodoListComponent } from './todo-list.component';

describe('TodoListComponent', () => {
let component: TodoListComponent;
let fixture: ComponentFixture<TodoListComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TodoListComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TodoListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/todo-list/todo-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-todo-list',
templateUrl: './todo-list.component.html',
styleUrls: ['./todo-list.component.css']
})
export class TodoListComponent implements OnInit {

constructor() { }

ngOnInit(): void {
}

}

0 comments on commit ec2b8bd

Please sign in to comment.