erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

54 rindas
1.6 KiB

pirms 5 gadiem
  1. -module(utHttpOn).
  2. %% API
  3. -export([]).
  4. %% API
  5. -export([
  6. req/6, %% Post, Put, Delete Only
  7. req_get/3, %% Get Only
  8. url_encode/1
  9. ]).
  10. -export([get_unique_ref/1, get_conv_ref/0]).
  11. get_conv_ref() ->
  12. get_unique_ref(20).
  13. get_unique_ref(Length) ->
  14. IntPass = random_num(36, Length),
  15. list_to_binary(lists:flatten(io_lib:format("~.36B", [IntPass]))).
  16. random_num(NumeralSystemBase, Length) ->
  17. Min = round(math:pow(NumeralSystemBase, Length - 1)),
  18. Max = round(math:pow(NumeralSystemBase, Length)),
  19. crypto:rand_uniform(Min, Max).
  20. req(Method, Url, Headers, ContType, Body, HttpOpts)
  21. when Method == post orelse Method == put orelse Method == delete ->
  22. HttpClOpts = [{sync, true}, {body_format, binary}],
  23. Resp = httpc:request(Method, {eu_types:to_list(Url), Headers, ContType, Body}, HttpOpts, HttpClOpts),
  24. minimize_resp(Resp).
  25. req_get(Url, Headers, HttpOpts) ->
  26. HttpClOpts = [{sync, true}, {body_format, binary}],
  27. Resp = httpc:request(get, {eu_types:to_list(Url), Headers}, HttpOpts, HttpClOpts),
  28. minimize_resp(Resp).
  29. minimize_resp(Resp) ->
  30. case Resp of
  31. {ok, {{_NewVrsn, 200, _}, _Headers, RespBody}} ->
  32. {ok, 200, RespBody};
  33. {ok, {{_NewVrsn, HttpCode, _}, _Headers, RespBody}} ->
  34. {error, HttpCode, RespBody};
  35. Any -> Any
  36. end.
  37. url_encode(Data) ->
  38. url_encode(Data, "").
  39. url_encode([], Acc) ->
  40. Acc;
  41. url_encode([{Key, Value} | R], "") ->
  42. url_encode(R, edoc_lib:escape_uri(Key) ++ "=" ++ edoc_lib:escape_uri(Value));
  43. url_encode([{Key, Value} | R], Acc) ->
  44. url_encode(R, Acc ++ "&" ++ edoc_lib:escape_uri(Key) ++ "=" ++ edoc_lib:escape_uri(Value)).