#loan #api #api-client #supremo

supremo_loan

Supremo贷款API

7个版本

0.1.6 2023年9月25日
0.1.5 2023年9月23日

#348 in #api-client

每月24次下载

MIT许可证

41KB
709

Supremo贷款API实现

这是Supremo贷款API的官方实现。它是对Supremo贷款API的封装。它提供了一个简单的方法,通过Rust与Supremo贷款API交互。

入门

我们还没有公开的测试API。您可以通过联系Supremo贷款来获取对API的访问权限。

用法

use supremo_loan::api::actions::{add_clients_keys, create_clients};

fn main() {
    let clients_json = serde_json::json!([{
        "base_url": "base_url",
        "name" : "bank name",
        "logo_url" : "logo_url",
        "redirect_url" : "redirect_url"
    }]);
    env::set_var("BANK_NAME_SECRET_KEY", "secret_key"); //this should be set in the environment from .env file to avoid leaking the secret key
    env::set_var("BANK_NAME_PUBLIC_KEY", "public_key"); //this should be set in the environment from .env file
    let clients = add_clients_keys(&clients_json).unwrap();
    let clients = create_clients(&clients).unwrap();
    let clients_json = serde_json::to_string(&clients).unwrap();
    assert_eq!(
        clients_json,
        r#"[{"base_url":"base_url","public_key":"public_key","name":"bank name","logo_url":"logo_url","redirect_url":"redirect_url"}]"#
    );
}

授权用户

为了让用户将贷款账户与您的应用链接,他们需要从前端应用中获取LENDER_API_BASE_URL/api/v1/oauth/auth/authorize?response_type=code&client_id={YOUR_CLIENT_ID}端点代码。

然后,将代码交换为以下所示的账户信息,将USER_CODE替换为从上述端点获得的代码。

use supremo_loan::api::actions::{add_clients_keys, create_clients};

fn main() {
    // ...
    //...
    let access_bank = clients[1]
    let user = access_bank.exchange_code_auth("USER_CODE");
        match user {
            Ok(user) => {
                println!("user {:?}", user);
                // use the user to do whatever you want such as connect to their account in your app
            }
            Err(e) => {
                println!("error {:?}", e.to_string());
                // handle error maybe ask user to get code again
            }
        }
}

其他辅助函数


    // as you see from above example `clients` is vector while here we are using a single client
    let client_json = serde_json::json!({
        "base_url": "base_url",
        "name" : "bank name",
        // add other client fields needed
    });
    let clients = create_client(&client_json).unwrap();


其他客户端方法

一旦您正确设置了客户端,您就可以访问其他方法,例如client_limitget_anchorsget_auth_token,如下所示,这有助于更快地完成任务

    // ...
    // create client
    let clients = create_client(&client).unwrap();


    // get bearer token to use to perform other actions
    let ouath = client.get_auth_token();


    // get user account loan limits
    let limits = client.client_limit("token");
    // `token` is access_token from oauth.access_token

    // use limit to make your app even faster by knowing when loan calculation/application will fail before
    // making the call (you could fetch this once a day) info will not be guarenteed to be up to date at time of
    // loan calculation/application as it might have changed from the time you fetched it


    // get client anchors
    let anchors = client.get_anchors(user.id, "token" Some(AnchorPagination{page: Some(10), page_size : Some(10), order :Some("-id")}));


    // calculate loan
    let loan_input = Vec<LoanInput> = vec![LoanInput{
        amount : 1000, // amount to loan
        anchor_id : user.anchor_id, // this is the anchor id you get from `get_anchors` (anchor id must be ancho to the client)
        client_id : user.id, // this is the id you get from `exchange_code_auth`
        loan_term : 30, // number of days
        loan_type : "api_request".to_string(), // needs to be api_request
        metadata : serde_json::json!({}), // this is optional data you need to send
        }]; //you could send multiple loans at once
    let loans_charges = client.calculate_loan("token", loan_input);


    // create loan
    let loan_res = client.apply_for_loan("token", loan_input);

如果名称是例如银行名称,则秘密密钥环境变量应为BANK_NAME_SECRET_KEY,公开密钥环境变量应为BANK_NAME_PUBLIC_KEY。[BANK_NAME]应替换为银行或贷款机构的名称。

从上述示例中,add_clients_keys函数接收一个客户端json数组,并将公开和秘密密钥添加到每个客户端对象中。create_clients函数接收一个客户端json数组,并在Supremo贷款API中创建每个客户端。

正如示例所示,您可以创建多个客户端,这些客户端将被添加到Supremo贷款API中。

您添加的客户端需要使用Supremo贷款API客户端库才能与Supremo贷款API交互。

查看Supremo贷款API客户端

注意:您需要为要添加到Supremo贷款API的每个客户端拥有秘密和公开密钥。

查找使用该软件的客户端。

许可证

MIT许可证

依赖关系

~22-36MB
~636K SLoC