-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #724: Push search data to server using api/push.json endpoint of server #753
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests technically do not test anything significant. How do you know that proper calls are made and functions are called accordingly? Please try to cover more cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to use already existing mock data inside |
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientModule | ||
], | ||
providers: [ | ||
PushService, | ||
HttpClient | ||
] | ||
}); | ||
}); | ||
|
||
it('should be created', inject([PushService], (service: PushService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 { statuses } = data; | ||
const searchMetadata = data.search_metadata; | ||
|
||
// Converting the object to JSON string. | ||
const dataToSend = JSON.stringify({search_metadata: searchMetadata, 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}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is otiose to push data to the backend when you are actually getting the data from the backend. It would already contain all the statuses in this case.
The push servlet is supposed to be used when we are fetching Tweets using Twitter API they may be new for the backend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree it would be more helpful once we complete the twitter api support. But till then it could work I guess, after that we can just push the mapped data from twitter api i.e. the data extracted from twitter api mapped in format of api.loklak. I have made all other changes, and I will surely write the tests in further patches. Thanks @singhpratyush.