Skip to content

Commit b97da3e

Browse files
Merge pull request #81 from delphi-hub/feature/postRequestWithGetParams
Post request with params
2 parents f9350cf + 6593fe3 commit b97da3e

File tree

5 files changed

+31
-54
lines changed

5 files changed

+31
-54
lines changed

app/controllers/ApiRouter.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ class ApiRouter @Inject()(irController: InstanceRegistryController, sysControlle
3636
case GET(p"/numberOfInstances" ? q"componentType=$componentType") => irController.numberOfInstances(componentType)
3737
case GET(p"/instances" ? q"componentType=$componentType") => irController.instances(componentType)
3838
case GET(p"/systemInfo") => sysController.getInfo()
39-
case POST(p"/postInstance") => irController.postInstance()
40-
case POST(p"/startInstance") => irController.handleRequest(action="/start")
41-
case POST(p"/stopInstance") => irController.handleRequest(action="/stop")
42-
case POST(p"/pauseInstance") => irController.handleRequest(action="/pause")
43-
case POST(p"/resumeInstance") => irController.handleRequest(action="/resume")
44-
case POST(p"/deleteInstance") => irController.handleRequest(action="/delete")
45-
39+
case POST(p"/postInstance" ? q"componentType=$componentType"& q"name=$name") => irController.postInstance(componentType, name)
40+
case POST(p"/startInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/start", instanceID)
41+
case POST(p"/stopInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/stop", instanceID)
42+
case POST(p"/pauseInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/pause", instanceID)
43+
case POST(p"/resumeInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/resume", instanceID)
44+
case POST(p"/deleteInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/delete", instanceID)
4645

4746
}
4847
}

app/controllers/InstanceRegistryController.scala

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
101101
*/
102102

103103

104-
def handleRequest(action: String): Action[AnyContent] = Action.async { request =>
105-
var instanceID = ""
106-
request.body.asJson.map { json =>
107-
for (jsonValue <- (json \ "params" \ "updates").as[JsValue].as[Seq[JsValue]]
108-
) {
109-
val obj = jsonValue.as[JsObject]
110-
if ((obj \ "param").asOpt[String].get.equals("InstanceID")) {
111-
instanceID = (obj \ "value").asOpt[String].get.substring(1)
112-
}
113-
}
114-
}
115-
104+
def handleRequest(action: String, instanceID: String): Action[AnyContent] = Action.async { request =>
116105
ws.url(instanceRegistryUri + action)
117106
.addQueryStringParameters("Id" -> instanceID)
118107
.post("")
@@ -132,23 +121,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
132121
* @param componentType
133122
* @param name
134123
*/
135-
def postInstance(): Action[AnyContent] = Action.async { request =>
136-
var compType = ""
137-
var name = ""
138-
139-
request.body.asJson.map { json =>
140-
for (jsonValue <- (json \ "params" \ "updates").as[JsValue].as[Seq[JsValue]]
141-
) {
142-
val obj = jsonValue.as[JsObject]
143-
if ((obj \ "param").asOpt[String].get.equals("componentType")) {
144-
compType = (obj \ "value").asOpt[String].get
145-
} else if ((obj \ "param").asOpt[String].get.equals("name")) {
146-
name = (obj \ "value").asOpt[String].get
147-
}
148-
}
149-
150-
}
151-
124+
def postInstance(compType: String, name: String): Action[AnyContent] = Action.async { request =>
152125
ws.url(instanceRegistryUri + "/deploy")
153126
.addQueryStringParameters("ComponentType" -> compType, "InstanceName" -> name)
154127
.post("")

client/src/app/api/api/api.service.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { Inject, Injectable, Optional } from '@angular/core';
20-
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
20+
import { HttpClient, HttpHeaders, HttpParams, HttpEvent } from '@angular/common/http';
2121
import { Observable } from 'rxjs';
2222
import { Configuration } from '../configuration';
2323
import {
@@ -81,7 +81,8 @@ export class ApiService {
8181
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
8282
* @param reportProgress flag to report request and response progress.
8383
*/
84-
public getNumberOfInstances(componentType: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
84+
public getNumberOfInstances(componentType: string, observe: any = 'body', reportProgress: boolean = false):
85+
Observable<HttpEvent<number>> {
8586
return this.get(NUMBER_OF_INSTANCES, componentType);
8687
}
8788

@@ -114,7 +115,7 @@ export class ApiService {
114115
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
115116
* @param reportProgress flag to report request and response progress.
116117
*/
117-
public startInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
118+
public startInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<HttpEvent<number>> {
118119
return this.postAction(START_INSTANCE, instanceId);
119120
}
120121

@@ -124,7 +125,7 @@ export class ApiService {
124125
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
125126
* @param reportProgress flag to report request and response progress.
126127
*/
127-
public stopInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
128+
public stopInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<HttpEvent<number>> {
128129
return this.postAction(STOP_INSTANCE, instanceId);
129130
}
130131

@@ -134,7 +135,7 @@ export class ApiService {
134135
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
135136
* @param reportProgress flag to report request and response progress.
136137
*/
137-
public pauseInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
138+
public pauseInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<HttpEvent<number>> {
138139
return this.postAction(PAUSE_INSTANCE, instanceId);
139140
}
140141

@@ -144,7 +145,7 @@ export class ApiService {
144145
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
145146
* @param reportProgress flag to report request and response progress.
146147
*/
147-
public resumeInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
148+
public resumeInstance(instanceId: string, observe: any = 'body', reportProgress: boolean = false): Observable<HttpEvent<number>> {
148149
return this.postAction(RESUME_INSTANCE, instanceId);
149150
}
150151

@@ -154,7 +155,7 @@ export class ApiService {
154155
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
155156
* @param reportProgress flag to report request and response progress.
156157
*/
157-
public deleteInstance(instanceId: string): Observable<number> {
158+
public deleteInstance(instanceId: string): Observable<HttpEvent<number>> {
158159
return this.postAction(DELETE_INSTANCE, instanceId);
159160
}
160161

@@ -212,7 +213,7 @@ export class ApiService {
212213
headers = headers.set('Accept', httpHeaderAcceptSelected);
213214
}
214215

215-
return this.httpClient.post<Instance>(`${this.basePath}${endpoint}`,
216+
return this.httpClient.post<Instance>(`${this.basePath}${endpoint}`, {},
216217
{
217218
params: queryParameters,
218219
withCredentials: this.configuration.withCredentials,
@@ -224,21 +225,23 @@ export class ApiService {
224225
}
225226

226227

227-
public postAction(endpoint: string, idInstance: string, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
228+
public postAction(endpoint: string, idInstance: string, observe: any = 'body', reportProgress: boolean = false):
229+
Observable<HttpEvent<number>> {
228230
let queryParam = new HttpParams({ encoder: new CustomHttpUrlEncodingCodec() });
229231

230232
if (idInstance === null || idInstance === undefined) {
231233
throw new Error('Required ID Instance parameter');
232234
} else {
233-
queryParam = queryParam.set('InstanceID', <any>('a' + idInstance));
235+
queryParam = queryParam.set('instanceID', <any>('a' + idInstance));
234236
}
235237

236238
return this.commonConf(endpoint, queryParam, observe, reportProgress);
237239
}
238240

239241

240242
// This method is a common configuration to set the headers and query params
241-
public commonConf(endpoint: string, queryParameters: HttpParams, observe: any = 'body', reportProgress: boolean = false): Observable<number> {
243+
public commonConf(endpoint: string, queryParameters: HttpParams, observe: any = 'body', reportProgress: boolean = false):
244+
Observable<HttpEvent<number>> {
242245
let headers = this.defaultHeaders;
243246

244247
// to determine the Accept header
@@ -251,7 +254,7 @@ export class ApiService {
251254
headers = headers.set('Accept', httpHeaderAcceptSelected);
252255
}
253256

254-
return this.httpClient.post<number>(`${this.basePath}${endpoint}`,
257+
return this.httpClient.post<number>(`${this.basePath}${endpoint}`, {},
255258
{
256259
params: queryParameters,
257260
withCredentials: this.configuration.withCredentials,

client/src/app/dashboard/dashboard-card/dashboard-card.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {EventType, EventTypeEnum} from '../../api/model/socketMessage';
2121
import {ComponentType, ComponentTypeEnum} from '../../api/model/instance';
2222
import {ApiService} from '../../api/api/api.service';
2323
import {SocketService} from '../../api/api/socket.service';
24+
import { HttpEvent } from '@angular/common/http';
2425

2526

2627

@@ -83,7 +84,7 @@ export class DashboardCardComponent implements OnInit {
8384
* If there is no server connection the value is set to a default error message.
8485
*/
8586
private setInstanceNumber() {
86-
this.irService.getNumberOfInstances(this.componentType).subscribe((amount: number) => {
87+
this.irService.getNumberOfInstances(this.componentType).subscribe((amount: HttpEvent<number>) => {
8788
this.numberOfInstances = '' + amount;
8889
}, () => {
8990
this.numberOfInstances = 'No server connection';

client/src/app/dashboard/table-all/table-all.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { MatDialog, MatDialogRef, MAT_DIALOG_DATA, MatTable, MatPaginator, MatTa
2222
import { DeleteDialogComponent } from '../delete-dialog/delete-dialog.component';
2323
import { AddDialogComponent } from '../add-dialog/add-dialog.component';
2424
import { ApiService } from '../../api/api/api.service';
25+
import { HttpEvent } from '@angular/common/http';
2526

2627

2728

@@ -77,7 +78,7 @@ export class TableAllComponent implements OnInit {
7778
console.log('data', this.dataSource.data);
7879

7980
} else {
80-
this.apiService.deleteInstance(id).subscribe((deleteResult: number) => {
81+
this.apiService.deleteInstance(id).subscribe((deleteResult: HttpEvent<number>) => {
8182
console.log('result', deleteResult);
8283
}, err => {
8384
console.log('error delete Instance');
@@ -133,7 +134,7 @@ export class TableAllComponent implements OnInit {
133134
*/
134135
public startInstance(id: string): void {
135136

136-
this.apiService.startInstance(id).subscribe((result: number) => {
137+
this.apiService.startInstance(id).subscribe((result: HttpEvent<number>) => {
137138
console.log('result', result);
138139
}, err => {
139140
console.log('error start Instance', err);
@@ -146,7 +147,7 @@ export class TableAllComponent implements OnInit {
146147
*/
147148
public stopInstance(id: string): void {
148149

149-
this.apiService.stopInstance(id).subscribe((result: number) => {
150+
this.apiService.stopInstance(id).subscribe((result: HttpEvent<number>) => {
150151
console.log('result', result);
151152
}, err => {
152153
console.log('error stop Instance', err);
@@ -159,7 +160,7 @@ export class TableAllComponent implements OnInit {
159160
*/
160161
public pauseInstance(id: string): void {
161162

162-
this.apiService.pauseInstance(id).subscribe((result: number) => {
163+
this.apiService.pauseInstance(id).subscribe((result: HttpEvent<number>) => {
163164
console.log('result', result);
164165
}, err => {
165166
console.log('error pause instance', err);
@@ -172,7 +173,7 @@ export class TableAllComponent implements OnInit {
172173
*/
173174
public resumeInstance(id: string): void {
174175

175-
this.apiService.resumeInstance(id).subscribe((result: number) => {
176+
this.apiService.resumeInstance(id).subscribe((result: HttpEvent<number>) => {
176177
console.log('result', result);
177178
}, err => {
178179
console.log('error pause instance', err);

0 commit comments

Comments
 (0)