Revision | 180 (tree) |
---|---|
Time | 2019-04-08 19:55:27 |
Author | hirukawa_ryo |
* rest-client-util 0.2.1
依存ライブラリ jackson-datetime-moduleのバージョンを0.1.1→0.1.2に変更しました。
Responseオブジェクトをtry-with-resources構文で扱うように変更しました。
@@ -735,25 +735,25 @@ | ||
735 | 735 | |
736 | 736 | public String get() throws IOException, HttpException { |
737 | 737 | Request request = createRequest(); |
738 | - Response response = getClient().newCall(request).execute(); | |
739 | - | |
740 | - if(!response.isSuccessful()) { | |
741 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
738 | + try(Response response = getClient().newCall(request).execute()) { | |
739 | + if(!response.isSuccessful()) { | |
740 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
741 | + } | |
742 | + | |
743 | + String value = response.body().string(); | |
744 | + return value; | |
742 | 745 | } |
743 | - | |
744 | - String value = response.body().string(); | |
745 | - return value; | |
746 | 746 | } |
747 | 747 | |
748 | 748 | public String getString() throws IOException, HttpException { |
749 | 749 | Request request = createRequest(); |
750 | - Response response = getClient().newCall(request).execute(); | |
751 | - | |
752 | - if(!response.isSuccessful()) { | |
753 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
750 | + try(Response response = getClient().newCall(request).execute()) { | |
751 | + if(!response.isSuccessful()) { | |
752 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
753 | + } | |
754 | + | |
755 | + return Server.getString(response); | |
754 | 756 | } |
755 | - | |
756 | - return Server.getString(response); | |
757 | 757 | } |
758 | 758 | |
759 | 759 | public String getString(String defaultValue) throws IOException, HttpException { |
@@ -766,13 +766,13 @@ | ||
766 | 766 | |
767 | 767 | public Boolean getBoolean() throws IOException, HttpException { |
768 | 768 | Request request = createRequest(); |
769 | - Response response = getClient().newCall(request).execute(); | |
770 | - | |
771 | - if(!response.isSuccessful()) { | |
772 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
769 | + try(Response response = getClient().newCall(request).execute()) { | |
770 | + if(!response.isSuccessful()) { | |
771 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
772 | + } | |
773 | + | |
774 | + return Server.getBoolean(response); | |
773 | 775 | } |
774 | - | |
775 | - return Server.getBoolean(response); | |
776 | 776 | } |
777 | 777 | |
778 | 778 | public boolean getBoolean(boolean defaultValue) throws IOException, HttpException { |
@@ -785,13 +785,13 @@ | ||
785 | 785 | |
786 | 786 | public Integer getInt() throws IOException, HttpException { |
787 | 787 | Request request = createRequest(); |
788 | - Response response = getClient().newCall(request).execute(); | |
789 | - | |
790 | - if(!response.isSuccessful()) { | |
791 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
788 | + try(Response response = getClient().newCall(request).execute()) { | |
789 | + if(!response.isSuccessful()) { | |
790 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
791 | + } | |
792 | + | |
793 | + return Server.getInteger(response); | |
792 | 794 | } |
793 | - | |
794 | - return Server.getInteger(response); | |
795 | 795 | } |
796 | 796 | |
797 | 797 | public int getInt(int defaultValue) throws IOException, HttpException { |
@@ -804,13 +804,13 @@ | ||
804 | 804 | |
805 | 805 | public Long getLong() throws IOException, HttpException { |
806 | 806 | Request request = createRequest(); |
807 | - Response response = getClient().newCall(request).execute(); | |
808 | - | |
809 | - if(!response.isSuccessful()) { | |
810 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
807 | + try(Response response = getClient().newCall(request).execute()) { | |
808 | + if(!response.isSuccessful()) { | |
809 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
810 | + } | |
811 | + | |
812 | + return Server.getLong(response); | |
811 | 813 | } |
812 | - | |
813 | - return Server.getLong(response); | |
814 | 814 | } |
815 | 815 | |
816 | 816 | public long getLong(long defaultValue) throws IOException, HttpException { |
@@ -823,13 +823,13 @@ | ||
823 | 823 | |
824 | 824 | public Float getFloat() throws IOException, HttpException { |
825 | 825 | Request request = createRequest(); |
826 | - Response response = getClient().newCall(request).execute(); | |
827 | - | |
828 | - if(!response.isSuccessful()) { | |
829 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
826 | + try(Response response = getClient().newCall(request).execute()) { | |
827 | + if(!response.isSuccessful()) { | |
828 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
829 | + } | |
830 | + | |
831 | + return Server.getFloat(response); | |
830 | 832 | } |
831 | - | |
832 | - return Server.getFloat(response); | |
833 | 833 | } |
834 | 834 | |
835 | 835 | public float getFloat(float defaultValue) throws IOException, HttpException { |
@@ -842,13 +842,13 @@ | ||
842 | 842 | |
843 | 843 | public Double getDouble() throws IOException, HttpException { |
844 | 844 | Request request = createRequest(); |
845 | - Response response = getClient().newCall(request).execute(); | |
846 | - | |
847 | - if(!response.isSuccessful()) { | |
848 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
845 | + try(Response response = getClient().newCall(request).execute()) { | |
846 | + if(!response.isSuccessful()) { | |
847 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
848 | + } | |
849 | + | |
850 | + return Server.getDouble(response); | |
849 | 851 | } |
850 | - | |
851 | - return Server.getDouble(response); | |
852 | 852 | } |
853 | 853 | |
854 | 854 | public double getDouble(double defaultValue) throws IOException, HttpException { |
@@ -861,69 +861,69 @@ | ||
861 | 861 | |
862 | 862 | public <T> T get(Class<T> returnType) throws IOException, HttpException { |
863 | 863 | Request request = createRequest(); |
864 | - Response response = getClient().newCall(request).execute(); | |
865 | - | |
866 | - if(!response.isSuccessful()) { | |
867 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
864 | + try(Response response = getClient().newCall(request).execute()) { | |
865 | + if(!response.isSuccessful()) { | |
866 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
867 | + } | |
868 | + | |
869 | + T value = null; | |
870 | + | |
871 | + InputStream in = response.body().byteStream(); | |
872 | + JsonParser parser = factory.createParser(in); | |
873 | + JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnType, mapper, parser); | |
874 | + if(iterator.hasNext()) { | |
875 | + value = iterator.next(); | |
876 | + } | |
877 | + return value; | |
868 | 878 | } |
869 | - | |
870 | - T value = null; | |
871 | - | |
872 | - InputStream in = response.body().byteStream(); | |
873 | - JsonParser parser = factory.createParser(in); | |
874 | - JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnType, mapper, parser); | |
875 | - if(iterator.hasNext()) { | |
876 | - value = iterator.next(); | |
877 | - } | |
878 | - return value; | |
879 | 879 | } |
880 | 880 | |
881 | 881 | public <T> List<T> getList(Class<T> returnClass) throws IOException, HttpException { |
882 | 882 | Request request = createRequest(); |
883 | - Response response = getClient().newCall(request).execute(); | |
884 | - | |
885 | - if(!response.isSuccessful()) { | |
886 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
883 | + try(Response response = getClient().newCall(request).execute()) { | |
884 | + if(!response.isSuccessful()) { | |
885 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
886 | + } | |
887 | + | |
888 | + InputStream in = response.body().byteStream(); | |
889 | + JsonParser parser = factory.createParser(in); | |
890 | + JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser); | |
891 | + List<T> list = new ArrayList<T>(); | |
892 | + while(iterator.hasNext()) { | |
893 | + T item = iterator.next(); | |
894 | + list.add(item); | |
895 | + } | |
896 | + return list; | |
887 | 897 | } |
888 | - | |
889 | - InputStream in = response.body().byteStream(); | |
890 | - JsonParser parser = factory.createParser(in); | |
891 | - JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser); | |
892 | - List<T> list = new ArrayList<T>(); | |
893 | - while(iterator.hasNext()) { | |
894 | - T item = iterator.next(); | |
895 | - list.add(item); | |
896 | - } | |
897 | - return list; | |
898 | 898 | } |
899 | 899 | |
900 | 900 | public <T> JsonObjectIterable<T> getIterable(Class<T> returnClass) throws IOException, HttpException { |
901 | 901 | Request request = createRequest(); |
902 | - Response response = getClient().newCall(request).execute(); | |
903 | - | |
904 | - if(!response.isSuccessful()) { | |
905 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
902 | + try(Response response = getClient().newCall(request).execute()) { | |
903 | + if(!response.isSuccessful()) { | |
904 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
905 | + } | |
906 | + | |
907 | + InputStream in = response.body().byteStream(); | |
908 | + JsonParser parser = factory.createParser(in); | |
909 | + JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response)); | |
910 | + JsonObjectIterable<T> iterable = new JsonObjectIterable<T>(iterator); | |
911 | + return iterable; | |
906 | 912 | } |
907 | - | |
908 | - InputStream in = response.body().byteStream(); | |
909 | - JsonParser parser = factory.createParser(in); | |
910 | - JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response)); | |
911 | - JsonObjectIterable<T> iterable = new JsonObjectIterable<T>(iterator); | |
912 | - return iterable; | |
913 | 913 | } |
914 | 914 | |
915 | 915 | public <T> JsonObjectIterator<T> getIterator(Class<T> returnClass) throws IOException, HttpException { |
916 | 916 | Request request = createRequest(); |
917 | - Response response = getClient().newCall(request).execute(); | |
918 | - | |
919 | - if(!response.isSuccessful()) { | |
920 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
917 | + try(Response response = getClient().newCall(request).execute()) { | |
918 | + if(!response.isSuccessful()) { | |
919 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
920 | + } | |
921 | + | |
922 | + InputStream in = response.body().byteStream(); | |
923 | + JsonParser parser = factory.createParser(in); | |
924 | + JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response)); | |
925 | + return iterator; | |
921 | 926 | } |
922 | - | |
923 | - InputStream in = response.body().byteStream(); | |
924 | - JsonParser parser = factory.createParser(in); | |
925 | - JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response)); | |
926 | - return iterator; | |
927 | 927 | } |
928 | 928 | |
929 | 929 | /* |
@@ -933,77 +933,77 @@ | ||
933 | 933 | */ |
934 | 934 | public Map<String, String> getMap() throws IOException, HttpException { |
935 | 935 | Request request = createRequest(); |
936 | - Response response = getClient().newCall(request).execute(); | |
937 | - | |
938 | - if(!response.isSuccessful()) { | |
939 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
936 | + try(Response response = getClient().newCall(request).execute()) { | |
937 | + if(!response.isSuccessful()) { | |
938 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
939 | + } | |
940 | + | |
941 | + Map<String, String> value = null; | |
942 | + | |
943 | + InputStream in = response.body().byteStream(); | |
944 | + JsonParser parser = factory.createParser(in); | |
945 | + | |
946 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
947 | + JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser); | |
948 | + if(iterator.hasNext()) { | |
949 | + value = iterator.next(); | |
950 | + } | |
951 | + return value; | |
940 | 952 | } |
941 | - | |
942 | - Map<String, String> value = null; | |
943 | - | |
944 | - InputStream in = response.body().byteStream(); | |
945 | - JsonParser parser = factory.createParser(in); | |
946 | - | |
947 | - @SuppressWarnings({ "rawtypes", "unchecked" }) | |
948 | - JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser); | |
949 | - if(iterator.hasNext()) { | |
950 | - value = iterator.next(); | |
951 | - } | |
952 | - return value; | |
953 | 953 | } |
954 | 954 | |
955 | 955 | public List<Map<String, String>> getMapList() throws IOException, HttpException { |
956 | 956 | Request request = createRequest(); |
957 | - Response response = getClient().newCall(request).execute(); | |
958 | - | |
959 | - if(!response.isSuccessful()) { | |
960 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
957 | + try(Response response = getClient().newCall(request).execute()) { | |
958 | + if(!response.isSuccessful()) { | |
959 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
960 | + } | |
961 | + | |
962 | + InputStream in = response.body().byteStream(); | |
963 | + JsonParser parser = factory.createParser(in); | |
964 | + | |
965 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
966 | + JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser); | |
967 | + List<Map<String, String>> list = new ArrayList<Map<String, String>>(); | |
968 | + while(iterator.hasNext()) { | |
969 | + Map<String, String> item = iterator.next(); | |
970 | + list.add(item); | |
971 | + } | |
972 | + return list; | |
961 | 973 | } |
962 | - | |
963 | - InputStream in = response.body().byteStream(); | |
964 | - JsonParser parser = factory.createParser(in); | |
965 | - | |
966 | - @SuppressWarnings({ "rawtypes", "unchecked" }) | |
967 | - JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser); | |
968 | - List<Map<String, String>> list = new ArrayList<Map<String, String>>(); | |
969 | - while(iterator.hasNext()) { | |
970 | - Map<String, String> item = iterator.next(); | |
971 | - list.add(item); | |
972 | - } | |
973 | - return list; | |
974 | 974 | } |
975 | 975 | |
976 | 976 | public JsonObjectIterable<Map<String, String>> getMapIterable() throws IOException, HttpException { |
977 | 977 | Request request = createRequest(); |
978 | - Response response = getClient().newCall(request).execute(); | |
979 | - | |
980 | - if(!response.isSuccessful()) { | |
981 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
978 | + try(Response response = getClient().newCall(request).execute()) { | |
979 | + if(!response.isSuccessful()) { | |
980 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
981 | + } | |
982 | + | |
983 | + InputStream in = response.body().byteStream(); | |
984 | + JsonParser parser = factory.createParser(in); | |
985 | + | |
986 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
987 | + JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response)); | |
988 | + JsonObjectIterable<Map<String, String>> iterable = new JsonObjectIterable<Map<String, String>>(iterator); | |
989 | + return iterable; | |
982 | 990 | } |
983 | - | |
984 | - InputStream in = response.body().byteStream(); | |
985 | - JsonParser parser = factory.createParser(in); | |
986 | - | |
987 | - @SuppressWarnings({ "rawtypes", "unchecked" }) | |
988 | - JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response)); | |
989 | - JsonObjectIterable<Map<String, String>> iterable = new JsonObjectIterable<Map<String, String>>(iterator); | |
990 | - return iterable; | |
991 | 991 | } |
992 | 992 | |
993 | 993 | public JsonObjectIterator<Map<String, String>> getMapIterator() throws IOException, HttpException { |
994 | 994 | Request request = createRequest(); |
995 | - Response response = getClient().newCall(request).execute(); | |
996 | - | |
997 | - if(!response.isSuccessful()) { | |
998 | - throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
995 | + try(Response response = getClient().newCall(request).execute()) { | |
996 | + if(!response.isSuccessful()) { | |
997 | + throw HttpException.byStatus(response.code(), response.message(), response.body().string()); | |
998 | + } | |
999 | + | |
1000 | + InputStream in = response.body().byteStream(); | |
1001 | + JsonParser parser = factory.createParser(in); | |
1002 | + | |
1003 | + @SuppressWarnings({ "rawtypes", "unchecked" }) | |
1004 | + JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response)); | |
1005 | + return iterator; | |
999 | 1006 | } |
1000 | - | |
1001 | - InputStream in = response.body().byteStream(); | |
1002 | - JsonParser parser = factory.createParser(in); | |
1003 | - | |
1004 | - @SuppressWarnings({ "rawtypes", "unchecked" }) | |
1005 | - JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response)); | |
1006 | - return iterator; | |
1007 | 1007 | } |
1008 | 1008 | } |
1009 | 1009 |
@@ -1020,7 +1020,7 @@ | ||
1020 | 1020 | @Override |
1021 | 1021 | public Request authenticate(Route route, Response response) throws IOException { |
1022 | 1022 | String credential = Credentials.basic(username, password); |
1023 | - return response.request().newBuilder().header("Authorization", credential).build(); | |
1023 | + return response.request().newBuilder().header("Authorization", credential).build(); | |
1024 | 1024 | } |
1025 | 1025 | } |
1026 | 1026 | } |