Skip to content

Commit 5a15a9c

Browse files
committed
implement upload richmenu content api
1 parent e815628 commit 5a15a9c

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

lib/client.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { get, post, stream, delete as deleteRequest } from "./http";
1+
import { Readable } from "stream";
2+
import { get, post, stream, delete as deleteRequest, postBinary } from "./http";
23
import * as Types from "./types";
34
import * as URL from "./urls";
4-
import { toArray } from "./util";
5+
import { toArray, detectContentType } from "./util";
56

67
export default class Client {
78
public config: Types.ClientConfig;
@@ -136,7 +137,13 @@ export default class Client {
136137
return this.stream(URL.richMenuContent(richMenuId));
137138
}
138139

139-
// TODO: implement method for uploading richmenu image
140+
public uploadRichMenuContent(
141+
richMenuId: string,
142+
data: Buffer | Readable,
143+
contentType?: string,
144+
): Promise<any> {
145+
return this.postBinary(URL.richMenuContent(richMenuId), data, contentType);
146+
}
140147

141148
public getRichMenuList(): Promise<any> {
142149
return this.get(URL.richMenuList());
@@ -158,6 +165,14 @@ export default class Client {
158165
return post(url, this.authHeader(), body);
159166
}
160167

168+
private postBinary(
169+
url: string,
170+
data: Buffer | Readable,
171+
contentType?: string,
172+
) {
173+
return postBinary(url, this.authHeader(), data, contentType);
174+
}
175+
161176
private stream(url: string): Promise<NodeJS.ReadableStream> {
162177
return stream(url, this.authHeader());
163178
}

lib/http.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import axios, { AxiosError } from "axios";
2+
import { detectContentType } from "./util";
3+
import { Readable } from "stream";
24
import {
35
HTTPError,
46
JSONParseError,
@@ -65,6 +67,24 @@ export function post(url: string, headers: any, data?: any): Promise<any> {
6567
.catch(wrapError);
6668
}
6769

70+
export function postBinary(
71+
url: string,
72+
headers: any,
73+
data: Buffer | Readable,
74+
contentType?: string,
75+
): Promise<Readable> {
76+
return new Promise(resolve => {
77+
return resolve(contentType ? contentType : detectContentType(data));
78+
}).then((contentType: string) => {
79+
headers["Content-Type"] = contentType;
80+
headers["User-Agent"] = userAgent;
81+
const responseType = "stream";
82+
return axios
83+
.post(url, data, { headers, responseType })
84+
.then(res => res.data);
85+
});
86+
}
87+
6888
function del(url: string, headers: any): Promise<any> {
6989
headers["User-Agent"] = userAgent;
7090

test/client.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readFileSync } from "fs";
2+
import { join } from "path";
13
import { deepEqual, equal } from "assert";
24
import Client from "../lib/client";
35
import * as Types from "../lib/types";
@@ -219,6 +221,32 @@ describe("client", () => {
219221
});
220222
});
221223

224+
it("uploadRichMenuContent", () => {
225+
const filepath = join(__dirname, "/helpers/LINE_Icon.png");
226+
const buffer = readFileSync(filepath);
227+
return client
228+
.uploadRichMenuContent("test_rich_menu_id", buffer)
229+
.then(s => getStreamData(s))
230+
.then((data: string) => {
231+
const res = JSON.parse(data);
232+
equal(res.headers.authorization, "Bearer test_channel_access_token");
233+
equal(res.path, "/richmenu/test_rich_menu_id/content");
234+
equal(res.method, "POST");
235+
});
236+
});
237+
238+
it("getRichMenuContent", () => {
239+
return client
240+
.getRichMenuContent("test_rich_menu_id")
241+
.then((s: NodeJS.ReadableStream) => getStreamData(s))
242+
.then((data: string) => {
243+
const res = JSON.parse(data);
244+
equal(res.headers.authorization, "Bearer test_channel_access_token");
245+
equal(res.path, "/richmenu/test_rich_menu_id/content");
246+
equal(res.method, "GET");
247+
});
248+
});
249+
222250
it("getRichMenuList", () => {
223251
return client.getRichMenuList().then((res: any) => {
224252
equal(res.headers.authorization, "Bearer test_channel_access_token");

test/http.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { deepEqual, equal, ok } from "assert";
22
import { HTTPError, JSONParseError, RequestError } from "../lib/exceptions";
3-
import { get, post, stream, delete as deleteRequest } from "../lib/http";
3+
import {
4+
get,
5+
post,
6+
stream,
7+
delete as deleteRequest,
8+
postBinary,
9+
} from "../lib/http";
410
import { getStreamData } from "./helpers/stream";
511
import { close, listen } from "./helpers/test-server";
12+
import { readFileSync } from "fs";
13+
import { join } from "path";
614

715
const pkg = require("../package.json");
816

@@ -88,6 +96,25 @@ describe("http", () => {
8896
});
8997
});
9098

99+
it("postBinary", () => {
100+
const testHeaders = {
101+
"test-header-key": "Test-Header-Value",
102+
};
103+
104+
const filepath = join(__dirname, "/helpers/LINE_Icon.png");
105+
const buffer = readFileSync(filepath);
106+
return postBinary(`${TEST_URL}/post`, testHeaders, buffer)
107+
.then(s => getStreamData(s))
108+
.then((data: string) => {
109+
const res = JSON.parse(data);
110+
equal(res.method, "POST");
111+
equal(res.path, "/post");
112+
equal(res.headers["test-header-key"], testHeaders["test-header-key"]);
113+
equal(res.headers["user-agent"], `${pkg.name}/${pkg.version}`);
114+
equal(res.headers["content-type"], "image/png");
115+
});
116+
});
117+
91118
it("fail to parse json", () => {
92119
return get(`${TEST_URL}/text`, {})
93120
.then(() => ok(false))

0 commit comments

Comments
 (0)