A service that posts JSON to a server and work with responses

EDIT: I included the code here instead of linking to a github repo

extern crate serde;

use serde::{Deserialize, Serialize};

use serde_json::json;

use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader};
use std::time::Duration;

const NER_SERVER: &'static str = "http://127.0.0.1:5000/parse";

#[derive(Serialize, Deserialize)]
struct Article {
    url: String,
    text: String,
    id: String,
}

#[derive(Serialize, Deserialize)]
struct NER {
    begin: u32,
    end: u32,
}

fn run() -> Result<u64, Box<Error>> {
    let file = File::open("data/test").unwrap();
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(100))
        .build()?;
    let reader = BufReader::new(file);
    let mut count : u64 = 0;
    for line in reader.lines() {
        let article: Article = serde_json::from_str(&line.unwrap_or_default())?;
        let ners: Vec<NER> = client.post(NER_SERVER)
            .json(&json!({"data": article.text}))
            .send()?.json()?; // TODO change to async
        for item in ners.iter() {
            println!("{} {}", item.begin, item.end);
        }
        count = count + 1;
    }
    Ok(count)
}

fn main() {
    match run() {
        Ok(e) => println!("{}", e),
        Err(e) => println!("{:#}", e)
    }
}

Piggyback question: How to set up a general JSON schema for the project?

Thank you all

Is there a question other than the piggyback question?

As far as the piggyback question, I’m not entirely sure what you’re asking. Do you have control over the json schema? It seems like you’ve got one set of json coming in and a different one going out.

I would like to have a central place for Json schema instead of having them in the Python code and Rust code. This way, I and other developers can just import the schema and use them directly instead of having to refer to additional documentation.

I think OpenAPI might be what you're looking for?

It looks like GitHub - OpenAPITools/openapi-generator: OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3) has implementations for generating both rust code and python code. If you can write your schema as a OpenAPI schema, then you should be able to generate the API code for both python and rust.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.