Skip to content

Commit 8f4c1c3

Browse files
authored
Merge pull request #99 from companieshouse/update-search-post-to-get
Update SDK to use GET method instead of POST
2 parents 3d612c8 + 8804c8e commit 8f4c1c3

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

src/http/http-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default interface IHttpClient {
6363
*
6464
* @param url the url relative to the base url
6565
*/
66-
httpGet(url: string): Promise<HttpResponse>;
66+
httpGet(url: string, headers?: Headers): Promise<HttpResponse>;
6767
httpPost(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
6868
httpPatch(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
6969
httpPut(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
@@ -122,7 +122,7 @@ export abstract class AbstractClient implements IHttpClient {
122122
*
123123
* @param url the full url to make a request to
124124
*/
125-
public abstract httpGet(url: string): Promise<HttpResponse>;
125+
public abstract httpGet(url: string, headers?: Headers): Promise<HttpResponse>;
126126

127127
public abstract httpPost(url: string, body?: any, headers?: Headers): Promise<HttpResponse>;
128128

src/http/request-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import rq from "request-promise-native";
66
* library.
77
*/
88
export default class RequestClient extends AbstractClient {
9-
public async httpGet (url: string): Promise<HttpResponse> {
10-
return this.request({ method: "GET", url });
9+
public async httpGet (url: string, headers?: Headers): Promise<HttpResponse> {
10+
return this.request({ method: "GET", url, headers });
1111
}
1212

1313
public async httpPost (url: string, body?: any, headers?: Headers): Promise<HttpResponse> {

src/services/search/alphabetical-search/service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { IHttpClient } from "../../../http";
2-
import { AlphabeticalSearchPostRequest, CompaniesResource } from "./types";
2+
import { CompaniesResource } from "./types";
33
import Resource from "../../resource";
44

55
export default class AlphabeticalSearchService {
66
constructor (private readonly client: IHttpClient) { }
7-
public async getCompanies (companyName: AlphabeticalSearchPostRequest, requestId: string): Promise<Resource<CompaniesResource>> {
7+
public async getCompanies (companyName: string, requestId: string): Promise<Resource<CompaniesResource>> {
88
const additionalHeaders = {
99
"X-Request-ID": requestId,
1010
"Content-Type": "application/json"
1111
}
12+
const alphabeticalSearchURL = "/alphabetical-search/companies?q=" + companyName;
1213

13-
const resp = await this.client.httpPost("/alphabetical-search/corporate-name", companyName, additionalHeaders);
14+
const resp = await this.client.httpGet(alphabeticalSearchURL, additionalHeaders);
1415

1516
const resource: Resource<CompaniesResource> = {
1617
httpStatusCode: resp.status

src/services/search/alphabetical-search/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,3 @@ export interface Items {
2121
export interface Links {
2222
self: string;
2323
}
24-
25-
// AlphabeticalSearchPostRequest
26-
export interface AlphabeticalSearchPostRequest {
27-
company_name: string
28-
}

test/http/http-client.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestClient extends AbstractClient {
1919
throw new Error("Method not implemented.");
2020
}
2121

22-
public async httpGet (url: string): Promise<HttpResponse> {
22+
public async httpGet (url: string, headers: any): Promise<HttpResponse> {
2323
return { status: 200 };
2424
}
2525
}

test/services/search/alphabetical-search/service.spec.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sinon from "sinon";
33

44
import { AlphabeticalSearchService } from "../../../../src/services/search/alphabetical-search";
55
import { RequestClient } from "../../../../src/http";
6-
import { AlphabeticalSearchPostRequest, CompaniesResource } from "../../../../src/services/search/alphabetical-search/types";
6+
import { CompaniesResource } from "../../../../src/services/search/alphabetical-search/types";
77
import Resource from "../../../../src/services/resource";
88
const expect = chai.expect;
99

@@ -29,13 +29,10 @@ const mockResponseBody : CompaniesResource = ({
2929
]
3030
});
3131

32-
const mockRequestBody: AlphabeticalSearchPostRequest = ({
33-
company_name: "TEST"
34-
});
35-
3632
const mockRequestId = "fdskfhsdoifhsffsif";
33+
const testCompanyName = "TEST COMPANY NAME";
3734

38-
describe("create a alphabetical search POST", () => {
35+
describe("create a alphabetical search GET", () => {
3936
beforeEach(() => {
4037
sinon.reset();
4138
sinon.restore();
@@ -48,28 +45,28 @@ describe("create a alphabetical search POST", () => {
4845
});
4946

5047
it("returns an error response on failure", async () => {
51-
const mockPostRequest = {
48+
const mockGetRequest = {
5249
status: 401,
5350
error: "An error occurred"
5451
};
5552

56-
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostRequest);
53+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetRequest);
5754
const search: AlphabeticalSearchService = new AlphabeticalSearchService(requestClient);
58-
const data: Resource<CompaniesResource> = await search.getCompanies(mockRequestBody, mockRequestId);
55+
const data: Resource<CompaniesResource> = await search.getCompanies(testCompanyName, mockRequestId);
5956

6057
expect(data.httpStatusCode).to.equal(401);
6158
expect(data.resource).to.be.undefined;
6259
});
6360

6461
it("returns alphabetical search results correctly", async () => {
65-
const mockPostRequest = {
62+
const mockGetRequest = {
6663
status: 200,
6764
body: mockResponseBody
6865
};
6966

70-
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostRequest);
67+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetRequest);
7168
const search: AlphabeticalSearchService = new AlphabeticalSearchService(requestClient);
72-
const data: Resource<CompaniesResource> = await search.getCompanies(mockRequestBody, mockRequestId);
69+
const data: Resource<CompaniesResource> = await search.getCompanies(testCompanyName, mockRequestId);
7370

7471
expect(data.httpStatusCode).to.equal(200);
7572
expect(data.resource.topHit).to.equal(mockResponseBody.topHit);

0 commit comments

Comments
 (0)