Skip to content

Commit

Permalink
refactor: Rename get_rdf() function to rdf()
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Mar 7, 2024
1 parent 63c49d8 commit bb28299
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [linux, macos, windows]
os: [linux, macos] # windows
target: [x86_64, aarch64]
manylinux: [auto]
include:
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.to_str()
.ok_or_else(|| NpError("Error getting signed path".to_string()))?
);
let _ = fs::write(signed_path, np.get_rdf()?);
let _ = fs::write(signed_path, np.rdf()?);
}
Some(("publish", sub)) => {
let orcid = "http://orcid.org/0000-0000-0000-0000";
Expand Down
4 changes: 2 additions & 2 deletions js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ <h1 class="text-xl font-semibold">Nanopublication dev</h1>
// const np = await new Nanopub(rdfStr).publish();

console.log("Published Nanopub:", np.info());
rdfOutput.innerText = np.getRdf();
rdfOutput.innerText = np.rdf();

// new Nanopub(rdfStr).publish(profile, "")
// .then(np => {
// rdfOutput.innerText = np.get_rdf();
// rdfOutput.innerText = np.rdf();
// console.log("Published Nanopub:", np.info());
// })
})
Expand Down
8 changes: 3 additions & 5 deletions js/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ impl Nanopub {
})
}

#[wasm_bindgen(js_name = getRdf)]
pub fn get_rdf(&self) -> Result<String, JsValue> {
self.np
.get_rdf()
.map_err(|e| JsValue::from_str(&e.to_string()))
#[wasm_bindgen(js_name = rdf)]
pub fn rdf(&self) -> Result<String, JsValue> {
self.np.rdf().map_err(|e| JsValue::from_str(&e.to_string()))
}

pub fn info(&self) -> Result<JsValue, JsValue> {
Expand Down
2 changes: 1 addition & 1 deletion lib/docs/docs/javascript-example-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ For example, to use it in a nextjs react app to publish a nanopub defined in JSO
const np = await new Nanopub(rdf).publish(profile, serverUrl)
console.log("Published info dict:", np.info());
setRdfOutput(np.get_rdf());
setRdfOutput(np.rdf());
});
}, []);
Expand Down
2 changes: 1 addition & 1 deletion lib/docs/docs/javascript-example-html.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You can easily import the NPM package from a CDN, and sign a Nanopublication fro
// Sign & publish a nanopub
const np = await new Nanopub(rdfStr).publish(profile, serverUrl);
console.log("Published info dict:", np.info());
rdfText.innerText = np.get_rdf();
rdfText.innerText = np.rdf();
}
main()
</script>
Expand Down
6 changes: 3 additions & 3 deletions lib/docs/docs/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This package provides several functionalities related to the handling of Nanopub
This process involves signing a Nanopublication RDF string using a specified RSA private key passed through the profile. The signing operation ensures that the Nanopub is authentically created by the holder of the private key.

!!! success "Get a private key"
You can easily create and register a new private key on the [playground page](https://vemonet.github.io/nanopub-rs/playground.html) after login with your ORCID.
You can easily create and register a new private key on the [playground page](https://vemonet.github.io/nanopub-rs/playground.html) after login with your ORCID.

```typescript
import init, { Nanopub, NpProfile, getNpServer } from "@nanopub/sign";
Expand Down Expand Up @@ -66,7 +66,7 @@ const profile = new NpProfile(privateKey, "https://orcid.org/0000-0000-0000-0000

// Sign the Nanopub RDF
const signed = new Nanopub(rdfStr).sign(profile);
console.log("Signed:", signed.info());
console.log("Signed:", signed.info(), signed.rdf());
```

### 📬 Publish Nanopubs
Expand All @@ -83,7 +83,7 @@ import { Nanopub, NpProfile } from "@nanopub/sign";

const profile = new NpProfile(privateKey, "https://orcid.org/0000-0000-0000-0000", "Your Name", "");
const np = await new Nanopub(rdfStr).publish(profile, null);
console.log("Published:", np.info());
console.log("Published:", np.info(), signed.rdf());
```

!!! tip "Provide the nanopub signed or unsigned"
Expand Down
6 changes: 3 additions & 3 deletions lib/docs/docs/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ <h1 class="text-xl font-semibold">✍️ Nanopublication signing playground 🕹
if (!publish) {
try {
const signed = new Nanopub(rdfStr).sign(profile)
oeditor.setValue(signed.getRdf());
oeditor.setValue(signed.rdf());
console.log("Signed Nanopub:", signed.info());
} catch (err) {
displayError(err);
Expand All @@ -189,7 +189,7 @@ <h1 class="text-xl font-semibold">✍️ Nanopublication signing playground 🕹
// Empty is test server
try {
const np = await new Nanopub(rdfStr).publish(profile, serverUrl);
oeditor.setValue(np.getRdf());
oeditor.setValue(np.rdf());
console.log("Published Nanopub:", np.info());
const npUrlElem = document.getElementById('np-url');
// const npUrl = (serverUrl) ? np.info().uri : `https://np.test.knowledgepixels.com/${np.info().trusty_hash}`
Expand Down Expand Up @@ -242,7 +242,7 @@ <h1 class="text-xl font-semibold">✍️ Nanopublication signing playground 🕹
// Publish nanopub link to public key
const intro = Nanopub.publish_intro(profile, getNpServer(false))
.then(np => {
oeditor.setValue(np.getRdf());
oeditor.setValue(np.rdf());
console.log("Published Introduction Nanopub:", np.info());
// Display private key
document.getElementById("privkey-card").classList.remove("hidden");
Expand Down
12 changes: 8 additions & 4 deletions lib/docs/docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

[![PyPI](https://img.shields.io/pypi/v/nanopub-sign)](https://pypi.org/project/nanopub-sign/)

You can use this toolkit to sign and publish Nanopublications from Python with the [`nanopub`](https://pypi.org/project/nanopub-sign/) pip package.
You can use this toolkit to sign and publish Nanopublications from Python with the [`nanopub-sign`](https://pypi.org/project/nanopub-sign/) pip package.

!!! info "Build a Nanopublication"
This package takes an already prepared Nanopublication RDF string as input. If you want to build a Nanopublication programmatically, use the [`nanopub`](https://fair-workflows.github.io/nanopub) pip package. You can then feed the serialized RDF of the built Nanopub to this package functions.

## 📥️ Install

Install the package with pip:
Install the package with pip or pipx:

```bash
pip install nanopub-sign
Expand Down Expand Up @@ -84,10 +84,12 @@ Use the `publish` function on a Nanopub, the 2 arguments are optional:
- `profile` is required if you want to also sign the nanopub, it is not required if you provide a signed nanopub
- If the `server_url` is null it will be published to the test server

```typescript
```python
from nanopub_sign import Nanopub, NpProfile, get_np_server

np = Nanopub(rdf_str).publish(profile=profile, server_url=None)
print("Published info dict:", np.info())
print(np.get_rdf())
print(np.rdf())
```

!!! tip "Provide the nanopub signed or unsigned"
Expand Down Expand Up @@ -117,6 +119,8 @@ np = Nanopub.publish(
This operation involves checking the integrity of Nanopubs. It ensures that a Nanopub is valid, regardless of whether it is signed or unsigned.

```python
from nanopub_sign import Nanopub

np = Nanopub(rdf_str).check()
print("Checked info dict:", np.info())
```
Expand Down
2 changes: 1 addition & 1 deletion lib/docs/docs/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let profile = NpProfile::new(&private_key, "https://orcid.org/0000-0000-0000-000
let signed_np = Nanopub::new(np_rdf).unwrap().sign(&profile).unwrap();

// Check
let checked_np = Nanopub::new(&signed_np.get_rdf().unwrap()).unwrap().check();
let checked_np = Nanopub::new(&signed_np.rdf().unwrap()).unwrap().check();

// Publish is async
let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use error::NpError;
/// let profile = NpProfile::new(&private_key, "https://orcid.org/0000-0000-0000-0000", "", None).unwrap();
///
/// let signed_np = Nanopub::new(&np_rdf).unwrap().sign(&profile).unwrap();
/// let checked_np = Nanopub::new(&signed_np.get_rdf().unwrap()).unwrap().check();
/// let checked_np = Nanopub::new(&signed_np.rdf().unwrap()).unwrap().check();
/// let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");
/// let published_np = rt.block_on(async {
/// Nanopub::new(&np_rdf).unwrap().publish(Some(&profile), None).await.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions lib/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct Nanopub {

impl fmt::Display for Nanopub {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "\n{:?}", self.get_rdf())?;
writeln!(f, "\n{:?}", self.rdf())?;
writeln!(f, "URI: {}", self.info.uri)?;
writeln!(f, "Trusty hash: {}", self.info.trusty_hash)?;
writeln!(f, "Signature hash: {}", self.info.signature)?;
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Nanopub {
if !self.info.signature.is_empty() {
println!("Nanopub already signed, unsigning it before re-signing");
self = self.unsign()?;
// println!("DEBUG: Unsigned: {}", self.get_rdf()?);
// println!("DEBUG: Unsigned: {}", self.rdf()?);
}

// Add triples about the signature in the pubinfo
Expand Down Expand Up @@ -391,7 +391,7 @@ impl Nanopub {
} else {
TEST_SERVER.to_string()
};
let published = publish_np(&server_url, &self.get_rdf()?).await?;
let published = publish_np(&server_url, &self.rdf()?).await?;
if published {
if TEST_SERVER == server_url {
self.info.published = Some(format!("{}{}", server_url, self.info.trusty_hash));
Expand Down Expand Up @@ -601,7 +601,7 @@ impl Nanopub {
}

/// Returns the RDF of the nanopub
pub fn get_rdf(&self) -> Result<String, NpError> {
pub fn rdf(&self) -> Result<String, NpError> {
serialize_rdf(&self.dataset, self.info.uri.as_str(), self.info.ns.as_str())
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/tests/nanopub_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn publish_nanopub_simple_rsa() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
println!("{}", np.get_rdf()?);
println!("{}", np.rdf()?);
assert!(np.info.published.is_some());
// Values compiled with the nanopub java lib using the exact same RDF
assert_eq!(
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn publish_already_signed_with_profile() -> Result<(), Box<dyn Error>> {
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
// println!("{}", np.info);
// println!("{}", np.get_rdf()?);
// println!("{}", np.rdf()?);
assert!(np.info.published.is_some());
Ok(())
}
Expand Down Expand Up @@ -227,7 +227,7 @@ async fn unit_publish_np_fail() -> Result<(), Box<dyn Error>> {
#[tokio::test]
async fn publish_from_scratch() -> Result<(), Box<dyn Error>> {
let mut np = Nanopub::new(create_base_dataset()?)?;
println!("DEBUG: SCRATCH {}", np.get_rdf()?);
println!("DEBUG: SCRATCH {}", np.rdf()?);
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
np.dataset.insert(
Iri::new_unchecked("http://example.org/mosquitoes"),
Expand All @@ -242,7 +242,7 @@ async fn publish_from_scratch() -> Result<(), Box<dyn Error>> {
Some(&np.info.prov),
)?;
let np = np.publish(Some(&profile), None).await?;
println!("DEBUG: SCRATCH 2 {}", np.get_rdf()?);
println!("DEBUG: SCRATCH 2 {}", np.rdf()?);
// assert!(res.is_err());
Ok(())
}
6 changes: 3 additions & 3 deletions python/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl NanopubPy {
}

#[pyo3(text_signature = "($self)")]
fn get_rdf(&self, _py: Python<'_>) -> PyResult<String> {
// py.allow_threads(|| Ok(self.np.get_rdf()))
fn rdf(&self, _py: Python<'_>) -> PyResult<String> {
// py.allow_threads(|| Ok(self.np.rdf()))
self.np
.get_rdf()
.rdf()
.map_err(|e| PyErr::new::<PyException, _>(format!("Error getting RDF: {e}")))
}

Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_nanopub.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_sign():
def test_publish():
np = Nanopub(rdf_str).publish(profile=profile, server_url=None)
# print("Published", np.info())
# print(np.get_rdf())
# print(np.rdf())
assert np.info()["trusty_hash"]
assert np.info()["published"]

Expand Down

0 comments on commit bb28299

Please sign in to comment.