使用R查询服务器,基于Python示例。

huangapple go评论140阅读模式
英文:

Querying a server with R based on a Python example

问题

我正在尝试将以下的Python脚本转换成R

  1. library(httr)
  2. library(jsonlite)
  3. get_telraam_data <- function(id = "9000001463",
  4. time_start = "2022-10-01 00:00:00Z",
  5. time_end = "2022-10-02 00:00:00Z",
  6. level = "segments",
  7. format = "per-hour",
  8. token = "***My_API-Key***") {
  9. url <- "https://telraam-api.net/v1/reports/traffic"
  10. body <- list(
  11. id = id,
  12. time_start = time_start,
  13. time_end = time_end,
  14. level = level,
  15. format = format
  16. )
  17. headers <- add_headers("X-Api-Key" = token)
  18. response <- POST(url, body = body, headers = headers, encode = "json")
  19. stop_for_status(response)
  20. json <- content(response, type = "text", encoding = "UTF-8") %>%
  21. fromJSON(flatten = TRUE)
  22. dataframe <- as.data.frame(json$report)
  23. write.csv(dataframe, file = "test.csv")
  24. }

其中的My_API-Key代表我的 API 密钥,我无法提供。

我使用了以下代码,但查询似乎不起作用:

  1. get_telraam_data()

我哪里出错了?
body 是否应该作为 query

英文:

I am trying to convert the following Python script in R:

  1. import pandas as pd
  2. import requests
  3. import json
  4. url = &quot;https://telraam-api.net/v1/reports/traffic&quot;
  5. body = {
  6. &quot;id&quot;:&quot;9000001463&quot;,
  7. &quot;time_start&quot;:&quot;2022-10-01 00:00:00Z&quot;,
  8. &quot;time_end&quot;:&quot;2022-10-02 00:00:00Z&quot;,
  9. &quot;level&quot;:&quot;segments&quot;,
  10. &quot;format&quot;:&quot;per-hour&quot;
  11. }
  12. headers = {
  13. &#39;X-Api-Key&#39;: &#39;***My_API-Key***&#39;
  14. }
  15. payload = str(body)
  16. response = requests.request(&quot;POST&quot;, url, headers=headers, data=payload)
  17. json = response.json()
  18. dataframe = pd.DataFrame(json[&#39;report&#39;])
  19. dataframe.to_csv(&#39;test.csv&#39;)

where My_API-Key stands for my API key which I cannot communicate.

I used the following:

  1. get_telraam_data &lt;- function(id = &quot;9000001463&quot;,
  2. time_start = &quot;2022-10-01 00:00:00Z&quot;,
  3. time_end = &quot;2022-10-02 00:00:00Z&quot;,
  4. level = &quot;segments&quot;,
  5. format = &quot;per-hour&quot;,
  6. token = &quot;***My_API-Key***&quot;) {
  7. GET(
  8. url = sprintf(&quot;https://telraam-api.net/v1&quot;),
  9. query = list(
  10. id = id,
  11. time_start = time_start,
  12. time_end = time_end,
  13. level = level,
  14. format = format
  15. ),
  16. content_type_json(),
  17. add_headers(&quot;X-Api-Key&quot; = token)
  18. ) -&gt; res
  19. stop_for_status(res)
  20. content(res, as=&quot;text&quot;, encoding=&quot;UTF-8&quot;) %&gt;%
  21. fromJSON(flatten=TRUE) %&gt;%
  22. as_tibble() %&gt;%
  23. readr::type_convert()
  24. }

but the query does not seem to work.

Where did I make an error?
Is body a query?

答案1

得分: 1

requests.request("POST", ...) 是一个 POST 调用,而 httr 的示例是使用 GET。您可以通过调用一些测试服务来比较请求/响应,例如 http://httpbin.org/。或者只需在您自己的计算机上打开一个套接字,以查看请求的差异。使用 httr,可以这样做:

  1. library(httr)
  2. id <- "9000001463"
  3. time_start <- "2022-10-01 00:00:00Z"
  4. time_end <- "2022-10-02 00:00:00Z"
  5. level <- "segments"
  6. format <- "per-hour"
  7. token <- "***My_API-Key***"
  8. api_endpoint <- "https://httpbin.org/post"
  9. # 或者只需在您自己的主机上运行netcat以检查请求:
  10. # nc -l 1234
  11. #api_endpoint <- "http://localhost:1234"
  12. POST(
  13. url = api_endpoint,
  14. body = list(
  15. id = id,
  16. time_start = time_start,
  17. time_end = time_end,
  18. level = level,
  19. format = format
  20. ),
  21. encode = "json",
  22. content_type_json(),
  23. add_headers("X-Api-Key" = token)
  24. )
  25. #> Response [https://httpbin.org/post]
  26. #> Date: 2023-05-28 19:15
  27. #> Status: 200
  28. #> Content-Type: application/json
  29. #> Size: 851 B
  30. #> {
  31. #> "args": {},
  32. #> "data": "{\"id\":\"9000001463\",\"time_start\":\"2022-10-01 00:00:00Z\",\"t...
  33. #> "files": {},
  34. #> "form": {},
  35. #> "headers": {
  36. #> "Accept": "application/json, text/xml, application/xml, */*",
  37. #> "Accept-Encoding": "deflate, gzip",
  38. #> "Content-Length": "128",
  39. #> "Content-Type": "application/json",
  40. #> ...
  41. #> }

虽然我个人会使用 httr2

  1. library(dplyr)
  2. library(httr2)
  3. response <- request(api_endpoint) %>%
  4. req_headers("X-Api-Key" = token) %>%
  5. req_body_json(list(
  6. id = id,
  7. time_start = time_start,
  8. time_end = time_end,
  9. level = level,
  10. format = format)) %>%
  11. # 要检查请求而不实际发送它:
  12. # req_dry_run()
  13. req_perform(verbosity = 2) %>%
  14. resp_body_json(simplifyVector = TRUE)
  15. #> -> POST /post HTTP/1.1
  16. #> -> Host: httpbin.org
  17. #> -> User-Agent: httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0
  18. #> -> Accept: */*
  19. #> -> Accept-Encoding: deflate, gzip
  20. #> -> X-Api-Key: ***My_API-Key***
  21. #> -> Content-Type: application/json
  22. #> -> Content-Length: 128
  23. #> ->
  24. #> >> {"id":"9000001463","time_start":"2022-10-01 00:00:00Z","time_end":"2022-10-02 00:00:00Z","level":"segments","format":"per-hour"}
  25. #> <- HTTP/1.1 200 OK
  26. #> <- Date: Sun, 28 May 2023 19:15:26 GMT
  27. #> <- Content-Type: application/json
  28. #> <- Content-Length: 807
  29. #> <- Connection: keep-alive
  30. #> <- Server: gunicorn/19.9.0
  31. #> <- Access-Control-Allow-Origin: *
  32. #> <- Access-Control-Allow-Credentials: true
  33. #> <-
  34. #> << {
  35. #> << "args": {},
  36. #> << "data": "{\"id\":\"9000001463\",\"time_start\":\"2022-10-01 00:00:00Z\",\"time_end\":\"2022-10-02 00:00:00Z\",\"level\":\"segments\",\"format\":\"per-hour\"}",
  37. #> << "files": {},
  38. #> << "form": {},
  39. #> << "headers": {
  40. #> << "Accept": "*/*",
  41. #> << "Accept-Encoding": "deflate, gzip",
  42. #> << "Content-Length": "128",
  43. #> << "Content-Type": "application/json",
  44. #> << "Host": "httpbin.org",
  45. #> << "User-Agent": "httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0",
  46. #> << "X-Amzn-Trace-Id": "Root=1-6473a84e-5ed51b7c52cac4ce2a19f855",
  47. #> << "X-Api-Key": "***My_API-Key***"
  48. #> << },
  49. #> << "json": {
  50. #> << "format": "per-hour",
  51. #> << "id": "9000001463",
  52. #> << "level": "segments",
  53. #> << "time_end": "2022-10-02 00:00:00Z",
  54. #> << "time_start": "2022-10-01 00:00:00Z"
  55. #> << },
  56. #> << "origin": "91.101.104.191",
  57. #> << "url": "https://httpbin.org/post"
  58. #> << }

创建于2023年05月28日,使用reprex v2.0.2

英文:

requests.request(&quot;POST&quot;, ...) is a POST call while httr example is using GET. You can compare requests / responses by making calls to some testing service, e.g. http://httpbin.org/ . Or just open up a socket on your own machine to see how requests differ. With httr, this should do:

  1. library(httr)
  2. id &lt;- &quot;9000001463&quot;
  3. time_start &lt;- &quot;2022-10-01 00:00:00Z&quot;
  4. time_end &lt;- &quot;2022-10-02 00:00:00Z&quot;
  5. level &lt;- &quot;segments&quot;
  6. format &lt;- &quot;per-hour&quot;
  7. token &lt;- &quot;***My_API-Key***&quot;
  8. api_endpoint &lt;- &quot;https://httpbin.org/post&quot;
  9. # or just run netcat on your own host to check the request:
  10. # nc -l 1234
  11. #api_endpoint &lt;- &quot;http://localhost:1234&quot;
  12. POST(
  13. url = api_endpoint,
  14. body = list(
  15. id = id,
  16. time_start = time_start,
  17. time_end = time_end,
  18. level = level,
  19. format = format
  20. ),
  21. encode = &quot;json&quot;,
  22. content_type_json(),
  23. add_headers(&quot;X-Api-Key&quot; = token)
  24. )
  25. #&gt; Response [https://httpbin.org/post]
  26. #&gt; Date: 2023-05-28 19:15
  27. #&gt; Status: 200
  28. #&gt; Content-Type: application/json
  29. #&gt; Size: 851 B
  30. #&gt; {
  31. #&gt; &quot;args&quot;: {},
  32. #&gt; &quot;data&quot;: &quot;{\&quot;id\&quot;:\&quot;9000001463\&quot;,\&quot;time_start\&quot;:\&quot;2022-10-01 00:00:00Z\&quot;,\&quot;t...
  33. #&gt; &quot;files&quot;: {},
  34. #&gt; &quot;form&quot;: {},
  35. #&gt; &quot;headers&quot;: {
  36. #&gt; &quot;Accept&quot;: &quot;application/json, text/xml, application/xml, */*&quot;,
  37. #&gt; &quot;Accept-Encoding&quot;: &quot;deflate, gzip&quot;,
  38. #&gt; &quot;Content-Length&quot;: &quot;128&quot;,
  39. #&gt; &quot;Content-Type&quot;: &quot;application/json&quot;,
  40. #&gt; ...

Though I'd personally use httr2:

  1. library(dplyr)
  2. library(httr2)
  3. response &lt;- request(api_endpoint) %&gt;%
  4. req_headers(&quot;X-Api-Key&quot; = token) %&gt;%
  5. req_body_json(list(
  6. id = id,
  7. time_start = time_start,
  8. time_end = time_end,
  9. level = level,
  10. format = format)) %&gt;%
  11. # to check request without actually sending it:
  12. # req_dry_run()
  13. req_perform(verbosity = 2) %&gt;%
  14. resp_body_json(simplifyVector = TRUE)
  15. #&gt; -&gt; POST /post HTTP/1.1
  16. #&gt; -&gt; Host: httpbin.org
  17. #&gt; -&gt; User-Agent: httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0
  18. #&gt; -&gt; Accept: */*
  19. #&gt; -&gt; Accept-Encoding: deflate, gzip
  20. #&gt; -&gt; X-Api-Key: ***My_API-Key***
  21. #&gt; -&gt; Content-Type: application/json
  22. #&gt; -&gt; Content-Length: 128
  23. #&gt; -&gt;
  24. #&gt; &gt;&gt; {&quot;id&quot;:&quot;9000001463&quot;,&quot;time_start&quot;:&quot;2022-10-01 00:00:00Z&quot;,&quot;time_end&quot;:&quot;2022-10-02 00:00:00Z&quot;,&quot;level&quot;:&quot;segments&quot;,&quot;format&quot;:&quot;per-hour&quot;}
  25. #&gt; &lt;- HTTP/1.1 200 OK
  26. #&gt; &lt;- Date: Sun, 28 May 2023 19:15:26 GMT
  27. #&gt; &lt;- Content-Type: application/json
  28. #&gt; &lt;- Content-Length: 807
  29. #&gt; &lt;- Connection: keep-alive
  30. #&gt; &lt;- Server: gunicorn/19.9.0
  31. #&gt; &lt;- Access-Control-Allow-Origin: *
  32. #&gt; &lt;- Access-Control-Allow-Credentials: true
  33. #&gt; &lt;-
  34. #&gt; &lt;&lt; {
  35. #&gt; &lt;&lt; &quot;args&quot;: {},
  36. #&gt; &lt;&lt; &quot;data&quot;: &quot;{\&quot;id\&quot;:\&quot;9000001463\&quot;,\&quot;time_start\&quot;:\&quot;2022-10-01 00:00:00Z\&quot;,\&quot;time_end\&quot;:\&quot;2022-10-02 00:00:00Z\&quot;,\&quot;level\&quot;:\&quot;segments\&quot;,\&quot;format\&quot;:\&quot;per-hour\&quot;}&quot;,
  37. #&gt; &lt;&lt; &quot;files&quot;: {},
  38. #&gt; &lt;&lt; &quot;form&quot;: {},
  39. #&gt; &lt;&lt; &quot;headers&quot;: {
  40. #&gt; &lt;&lt; &quot;Accept&quot;: &quot;*/*&quot;,
  41. #&gt; &lt;&lt; &quot;Accept-Encoding&quot;: &quot;deflate, gzip&quot;,
  42. #&gt; &lt;&lt; &quot;Content-Length&quot;: &quot;128&quot;,
  43. #&gt; &lt;&lt; &quot;Content-Type&quot;: &quot;application/json&quot;,
  44. #&gt; &lt;&lt; &quot;Host&quot;: &quot;httpbin.org&quot;,
  45. #&gt; &lt;&lt; &quot;User-Agent&quot;: &quot;httr2/0.2.2 r-curl/5.0.0 libcurl/7.84.0&quot;,
  46. #&gt; &lt;&lt; &quot;X-Amzn-Trace-Id&quot;: &quot;Root=1-6473a84e-5ed51b7c52cac4ce2a19f855&quot;,
  47. #&gt; &lt;&lt; &quot;X-Api-Key&quot;: &quot;***My_API-Key***&quot;
  48. #&gt; &lt;&lt; },
  49. #&gt; &lt;&lt; &quot;json&quot;: {
  50. #&gt; &lt;&lt; &quot;format&quot;: &quot;per-hour&quot;,
  51. #&gt; &lt;&lt; &quot;id&quot;: &quot;9000001463&quot;,
  52. #&gt; &lt;&lt; &quot;level&quot;: &quot;segments&quot;,
  53. #&gt; &lt;&lt; &quot;time_end&quot;: &quot;2022-10-02 00:00:00Z&quot;,
  54. #&gt; &lt;&lt; &quot;time_start&quot;: &quot;2022-10-01 00:00:00Z&quot;
  55. #&gt; &lt;&lt; },
  56. #&gt; &lt;&lt; &quot;origin&quot;: &quot;91.101.104.191&quot;,
  57. #&gt; &lt;&lt; &quot;url&quot;: &quot;https://httpbin.org/post&quot;
  58. #&gt; &lt;&lt; }

<sup>Created on 2023-05-28 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月29日 01:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定