From 5d8f93c8b8a2b7d31c7861e21e1f2ead28ea3027 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Wed, 2 Oct 2019 20:57:20 +0900 Subject: [PATCH] Add User-Agent header. --- lib/line_bot/api_client.ex | 9 ++++++++- lib/line_bot/token_server.ex | 11 ++++++++--- test/line_bot/api_client_test.exs | 14 ++++++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/line_bot/api_client.ex b/lib/line_bot/api_client.ex index a559147..a6f92d3 100644 --- a/lib/line_bot/api_client.ex +++ b/lib/line_bot/api_client.ex @@ -57,9 +57,16 @@ defmodule LineBot.APIClient do @doc """ Adds the OAuth Bearer token to the `Authorization` header. The token is retrieved by calling `LineBot.TokenServer.get_token/0`. + + Also adds the `User-Agent` header with a value of `line-botsdk-elixir/vX.X.X`. + This follows the pattern of Line Bot libraries in other languages. """ def process_request_headers(headers) do - [{"Authorization", "Bearer #{@token_server.get_token()}"} | super(headers)] + [ + {"Authorization", "Bearer #{@token_server.get_token()}"}, + {"User-Agent", "line-botsdk-elixir/v#{Application.spec(:line_bot, :vsn)}"} + | super(headers) + ] end @impl HTTPoison.Base diff --git a/lib/line_bot/token_server.ex b/lib/line_bot/token_server.ex index e75198a..92a4465 100644 --- a/lib/line_bot/token_server.ex +++ b/lib/line_bot/token_server.ex @@ -68,7 +68,12 @@ defmodule LineBot.TokenServer do %{state | token: token, expires_on: expires_on} end - @headers [{"Content-Type", "application/x-www-form-urlencoded"}] + defp headers do + [ + {"Content-Type", "application/x-www-form-urlencoded"}, + {"User-Agent", "line-botsdk-elixir/v#{Application.spec(:line_bot, :vsn)}"} + ] + end defp post_request_token(client_id, client_secret) do data = [ @@ -79,7 +84,7 @@ defmodule LineBot.TokenServer do %{"access_token" => access_token, "expires_in" => expires_in} = "https://api.line.me/v2/oauth/accessToken" - |> @http.post!({:form, data}, @headers) + |> @http.post!({:form, data}, headers()) |> Map.get(:body) |> Jason.decode!() @@ -90,7 +95,7 @@ defmodule LineBot.TokenServer do @http.post!( "https://api.line.me/v2/oauth/revoke", {:form, [access_token: access_token]}, - @headers + headers() ) end diff --git a/test/line_bot/api_client_test.exs b/test/line_bot/api_client_test.exs index b7ea960..b9e87c6 100644 --- a/test/line_bot/api_client_test.exs +++ b/test/line_bot/api_client_test.exs @@ -33,14 +33,20 @@ defmodule LineBot.APIClientTest do assert %{"foo" => "bar"} == APIClient.process_response(response).body end - test "process_request_headers adds auth header from token server" do + test "process_request_headers adds auth and user-agent headers" do expect(MockTokenServer, :get_token, fn -> "dummy_token" end) expect(MockTokenServer, :get_token, fn -> "changed_token" end) - assert [{"Authorization", "Bearer dummy_token"}] == APIClient.process_request_headers([]) + assert [ + {"Authorization", "Bearer dummy_token"}, + {"User-Agent", "line-botsdk-elixir/v" <> _} + ] = APIClient.process_request_headers([]) - assert [{"Authorization", "Bearer changed_token"}, {"other", "header"}] == - APIClient.process_request_headers([{"other", "header"}]) + assert [ + {"Authorization", "Bearer changed_token"}, + {"User-Agent", "line-botsdk-elixir/v" <> _}, + {"other", "header"} + ] = APIClient.process_request_headers([{"other", "header"}]) end # TODO these call super. Not sure how to test them without calling HTTPoison.