From 3d0006487200bb0c113c858679cb86d487695750 Mon Sep 17 00:00:00 2001 From: worldofpeace Date: Sun, 22 Dec 2019 14:36:01 -0500 Subject: [PATCH] nixpkgs-github-update: don't malform header --- .../github_latest_version.ex | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/pkgs/desktops/pantheon/nixpkgs_github_update/lib/nixpkgs_github_update/github_latest_version.ex b/pkgs/desktops/pantheon/nixpkgs_github_update/lib/nixpkgs_github_update/github_latest_version.ex index 76dcc2d69976..bd4bb8100ba5 100644 --- a/pkgs/desktops/pantheon/nixpkgs_github_update/lib/nixpkgs_github_update/github_latest_version.ex +++ b/pkgs/desktops/pantheon/nixpkgs_github_update/lib/nixpkgs_github_update/github_latest_version.ex @@ -3,15 +3,9 @@ defmodule NixpkgsGitHubUpdate.GitHubLatestVersion do def fetch({owner, repo}) do endpoint = releases_endpoint(owner, repo) + headers = construct_headers() - oauth_token = String.to_charlist("#{System.get_env("OAUTH_TOKEN")}") - - headers = %{ - 'User-Agent' => @user_agent, - 'Authorization' => 'token #{oauth_token}' - } - - :httpc.request(:get, {endpoint, Map.to_list(headers)}, [], []) + :httpc.request(:get, {endpoint, headers}, [], []) |> handle_response end @@ -19,6 +13,23 @@ defmodule NixpkgsGitHubUpdate.GitHubLatestVersion do 'https://api.github.com/repos/#{owner}/#{repo}/releases/latest' end + def construct_headers do + headers = %{'User-Agent' => @user_agent} + + put_token(headers, get_token()) + |> Map.to_list + end + + defp get_token do + String.to_charlist("#{System.get_env("OAUTH_TOKEN")}") + end + + defp put_token(headers, token) when is_binary(token) do + Map.put_new(headers, 'Authorization', 'token #{token}') + end + + defp put_token(headers, _), do: headers + def handle_response({_, {{_httpv, status_code, _}, _headers, response}}) do { status_code |> check_for_error(),