phoenix 開發API系列(二)phoenix 各類 api 實現方式


概述

上一篇已經提到如何安裝以及利用 phoenix framework 來開發一個簡單的 api。 接着上次的工程,下面演示如何通過 phoenix framework 來構建各種類型的 api 來應對前端的各類請求。

下面使用的工程的完整代碼已經公開在: http://git.oschina.net/wangyubin/phoenix-api

各類 api 的實現示例

restful url 的參數

introduce by code:

  • controller 中相關代碼:

    @doc "/api/param/:name"
    def rest_param1(conn, %{"name" => name}) do
      json conn, %{
        "result": "success",
        "message": "your name is " <> name,
      }
    end
    
    @doc "/api/param/:name/:age"
    def rest_param2(conn, %{"name" => name, "age" => age}) do
      json conn, %{
        "result": "success",
        "message": "your name is " <> name <> " and age is " <> age,
      }
    end
  • router 相關代碼: (router.ex)
    get "/param/:name", ApiParamController, :rest_param1
    get "/param/:name/:age", ApiParamController, :rest_param2
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

在 瀏覽器 中訪問 http://localhost:4000/api/param/wanghttp://localhost:4000/api/param/wang/33 可以看到返回的 json。

GET 請求中的參數

introduce by code: api的參數的上面的示例一樣

  • controller 中相關代碼:(api_param_controller.ex)
    @doc "/api/param?name=xxx&age=yyy"
    def rest_param3(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from rest_param3",
          "message": "your name is " <> params["name"] <> " and age is " <> params["age"],
        }
      else
        json conn, %{
          "result": "success from rest_param3",
          "message": "your name is " <> params["name"],
        }
      end
    end
  • router 相關代碼: (router.ex)
    get "/param", ApiParamController, :rest_param3
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

在 瀏覽器 中訪問 http://localhost:4000/api/param?name=wang&age=33http://localhost:4000/api/param?name=wang 可以看到返回的 json。

POST 請求中的參數

introduce by code: api的參數的上面的示例一樣

  • controller 中相關代碼:(api_param_controller.ex)
    @doc "/api/param"
    def post_param(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from post_param",
          "message": "your name is " <> params["name"] <> " and age is " <> params["age"],
        }
      else
        json conn, %{
          "result": "success from post_param",
          "message": "your name is " <> params["name"],
        }
      end
    end
  • router 相關代碼: (router.ex)
    post "/param", ApiParamController, :post_param
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

測試api 可以使用 curl 命令:

    curl -X POST -H "Cache-Control: no-cache" -F "name=wyb" "http://localhost:4000/api/param"
    curl -X POST -H "Cache-Control: no-cache" -F "name=wyb" -F "age=33" "http://localhost:4000/api/param"

json 格式參數

introduce by code: api的參數的上面的示例一樣

  • controller 中相關代碼:(api_param_controller.ex)
    @doc "/api/json-param"
    def json_param(conn, params) do
      if Map.has_key?(params, "age") do
        json conn, %{
          "result": "success from json_param",
          "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]),
        }
      else
        json conn, %{
          "result": "success from json_param",
          "message": "your name is " <> params["name"],
        }
      end
    end
  • router 相關代碼: (router.ex)
    post "/json-param", ApiParamController, :json_param
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

測試api 可以使用 curl 命令:

    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb"
    }' "http://localhost:4000/api/json-param"
    
    curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
        "name": "wyb",
        "age": 33
    }' "http://localhost:4000/api/json-param"

下載 文件

introduce by code: api的參數的上面的示例一樣

  • controller 中相關代碼:(api_param_controller.ex)
    @doc "/api/file-param"
    def file_param(conn, params) do
      filepath = "/tmp/downloadfile.txt"
      if Map.has_key?(params, "age") do
        File.write(filepath, "your name is " <> params["name"] <> " and age is " <> to_string(params["age"]))
      else
        File.write(filepath, "your name is " <> params["name"])
      end
    
      conn |> send_file(200, filepath)
    end
  • router 相關代碼: (router.ex)
    get "/file-param", ApiParamController, :file_param
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

在 瀏覽器 中訪問 http://localhost:4000/api/file-param?name=wang&age=33http://localhost:4000/api/file-param?name=wang 可以看到返回的 json。

上傳 文件

introduce by code: api的參數的上面的示例一樣

  • controller 中相關代碼:(api_param_controller.ex)
    @doc "/api/file-param"
    def upload_param(conn, params) do
    
      file = params["file"]
      File.cp(file.path, "/tmp/upload.file")
    
      json conn, %{
        "result": "success from file_param",
        "message": "your name is " <> params["name"] <> " and age is " <> to_string(params["age"])
        <> " and the filename which you upload is " <> file.filename,
      }
    end
  • router 相關代碼: (router.ex)
    post "/file-param", ApiParamController, :upload_param
  • 啟動 phoenix 開發服務器,就可以在瀏覽器中訪問對應的 URL
    mix phoenix.server

測試api 可以使用 curl 命令: 命令中的 file 要替換成你的實際文件路徑

    curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" \
        -F "name=wyb" -F "age=33" -F "file=@/tmp/test.jpg" "http://localhost:4000/api/file-param"

總結

可以看岀,phoenix framework 的 Plug 提供了豐富的功能,所以編寫 api 非常方便。 掌握了上面的示例,基本就可以滿足構建web服務時大部分的 api 的寫法了。

來源:http://blog.iotalabs.io/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM