-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #724: Push search data to server using api/push.json
- Loading branch information
1 parent
2559bbd
commit cc4f8c8
Showing
6 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface PushApiResponse { | ||
status: string; | ||
records: number; | ||
mps: number; | ||
message: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { HttpClientModule, HttpClient } from '@angular/common/http'; | ||
import { PushService } from './push.service'; | ||
|
||
describe('PushService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientModule | ||
], | ||
providers: [ | ||
PushService, | ||
HttpClient | ||
] | ||
}); | ||
}); | ||
|
||
it('should be created', inject([PushService], (service: PushService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpClient, | ||
HttpHeaders, | ||
HttpParams | ||
} from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { ApiResponse, PushApiResponse } from '../models'; | ||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
/* | ||
* The PushService is called on SEARCH_COMPLETE_SUCCESS | ||
* to push the result data back to server in the same | ||
* search result object format obtained from server. | ||
**/ | ||
export class PushService { | ||
|
||
constructor( private http: HttpClient ) { } | ||
|
||
// Returns an Observable of PushApiResponse. | ||
public postData(data: ApiResponse): Observable<PushApiResponse> { | ||
|
||
// End point to make a Post to. | ||
const httpUrl = 'https://api.loklak.org/api/push.json'; | ||
|
||
const headers = new HttpHeaders({ | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Accept': 'application/json', | ||
'cache-control': 'no-cache' | ||
}); | ||
// Extracting search_metadata and statuses | ||
// from the data obtained from search result. | ||
const {search_metadata, statuses} = data; | ||
|
||
// Converting the object to JSON string. | ||
const dataToSend = JSON.stringify({search_metadata: search_metadata, statuses}); | ||
|
||
// Setting the data to send in HttpParams() | ||
// with key as 'data' | ||
const body = new HttpParams().set('data', dataToSend); | ||
|
||
// Making a Post request to api/push.json endpoint | ||
// of server with required header and data body. | ||
// Response Object is converted to PushApiResponse type. | ||
return this.http.post<PushApiResponse>(httpUrl, body, {headers: headers}); | ||
} | ||
} |