-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: http + json extended example #91
Merged
Merged
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0a06414
add input
lonerapier 23d37c5
fix array index codegen bug
lonerapier 730c2c0
refactor: tests utils reorganise
lonerapier e87732a
add spotify test
lonerapier c683f3a
save work till now, start on integrated codegen
lonerapier 9efb1ca
save work
lonerapier 3bf9941
fix: tests
lonerapier d550b32
refactor: codegen
lonerapier 60e264a
add integrated codegen
lonerapier 0f17e7f
test: add separate test for correctness
lonerapier 760be60
refactor: change `Syntax` and `StateUpdate` to prevent duplicate call…
lonerapier 7250b02
feat: accept inputs in bytes for `readJSONInputFile`
lonerapier 3ccdc95
add extended witness gen in cli
lonerapier 9861bcf
feat: extended test works!!!
lonerapier d01c855
refactor: json cleanup
lonerapier 925bc61
add tests
lonerapier feabcf6
add test and lint CI
lonerapier 0113b1a
add http and extended codegen docs
lonerapier ef4ad06
add docs
lonerapier eadd686
rm old README info
Autoparallel f06ffb7
Update http.rs (#92)
Autoparallel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { toByte } from "."; | ||
import { join } from "path"; | ||
import { readFileSync } from "fs"; | ||
|
||
export function readLockFile<T>(filename: string): T { | ||
const filePath = join(__dirname, "..", "..", "..", "examples", "http", "lockfile", filename); | ||
const jsonString = readFileSync(filePath, 'utf-8'); | ||
const jsonData = JSON.parse(jsonString); | ||
return jsonData; | ||
} | ||
|
||
export function getHeaders(data: Request | Response): [string, string][] { | ||
const headers: [string, string][] = []; | ||
let i = 1; | ||
while (true) { | ||
const nameKey = `headerName${i}`; | ||
const valueKey = `headerValue${i}`; | ||
if (nameKey in data && valueKey in data) { | ||
headers.push([data[nameKey], data[valueKey]]); | ||
i++; | ||
} else { | ||
break; | ||
} | ||
} | ||
return headers; | ||
} | ||
|
||
export interface Request { | ||
method: string, | ||
target: string, | ||
version: string, | ||
[key: string]: string, | ||
} | ||
|
||
export interface Response { | ||
version: string, | ||
status: string, | ||
message: string, | ||
[key: string]: string, | ||
} | ||
|
||
export function readHTTPInputFile(filename: string) { | ||
const filePath = join(__dirname, "..", "..", "..", "examples", "http", filename); | ||
let data = readFileSync(filePath, 'utf-8'); | ||
|
||
let input = toByte(data); | ||
|
||
// Split headers and body, accounting for possible lack of body | ||
const parts = data.split('\r\n\r\n'); | ||
const headerSection = parts[0]; | ||
const bodySection = parts.length > 1 ? parts[1] : ''; | ||
|
||
// Function to parse headers into a dictionary | ||
function parseHeaders(headerLines: string[]) { | ||
const headers: { [id: string]: string } = {}; | ||
|
||
headerLines.forEach(line => { | ||
const [key, value] = line.split(/:\s(.+)/); | ||
if (key) headers[key] = value ? value : ''; | ||
}); | ||
|
||
return headers; | ||
} | ||
|
||
// Parse the headers | ||
const headerLines = headerSection.split('\r\n'); | ||
const initialLine = headerLines[0].split(' '); | ||
const headers = parseHeaders(headerLines.slice(1)); | ||
|
||
// Parse the body, if JSON response | ||
let responseBody = {}; | ||
if (headers["Content-Type"] == "application/json" && bodySection) { | ||
try { | ||
responseBody = JSON.parse(bodySection); | ||
} catch (e) { | ||
console.error("Failed to parse JSON body:", e); | ||
} | ||
} | ||
|
||
// Combine headers and body into an object | ||
return { | ||
input: input, | ||
initialLine: initialLine, | ||
headers: headers, | ||
body: responseBody, | ||
bodyBytes: toByte(bodySection || ''), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { circomkit, WitnessTester, toByte, readJSONInputFile } from "./common"; | ||
import { readLockFile, readHTTPInputFile, getHeaders as getHttpHeaders, Response } from "./common/http"; | ||
import { executeCodegen as httpLockfileCodegen } from "./http/codegen.test"; | ||
import { executeCodegen as jsonLockfileCodegen } from "./json/extractor/extractor.test"; | ||
|
||
describe("spotify top artists", async () => { | ||
let http_circuit: WitnessTester<["data", "version", "status", "message", "header1", "value1", "header2", "value2"], ["body"]>; | ||
let json_circuit: WitnessTester<["data", "key1", "key2", "key3", "key4", "key5", "key7", "key8", "key9"], ["value"]>; | ||
|
||
it("POST response body extraction", async () => { | ||
let httpLockfile = "spotify.lock" | ||
let httpInputFile = "spotify_top_artists_response.http"; | ||
let httpCircuitName = "spotify_top_artists"; | ||
|
||
await httpLockfileCodegen(httpCircuitName, httpInputFile, `${httpLockfile}.json`); | ||
|
||
let jsonFilename = "spotify"; | ||
|
||
await jsonLockfileCodegen(`${jsonFilename}_test`, `${jsonFilename}.json`, `${jsonFilename}.json`); | ||
|
||
let index_0 = 0; | ||
|
||
let [inputJson, key, output] = readJSONInputFile( | ||
`${jsonFilename}.json`, | ||
[ | ||
"data", | ||
"me", | ||
"profile", | ||
"topArtists", | ||
"items", | ||
index_0, | ||
"data", | ||
"profile", | ||
"name" | ||
] | ||
); | ||
|
||
// json_circuit = await circomkit.WitnessTester(`Extract`, { | ||
// file: `main/json_${jsonFilename}_test`, | ||
// template: "ExtractStringValue", | ||
// params: [input.length, 9, 4, 0, 2, 1, 7, 2, 10, 3, 5, 4, index_0, 5, 4, 6, 7, 7, 4, 8, 12], | ||
// }); | ||
// console.log("#constraints:", await json_circuit.getConstraintCount()); | ||
|
||
// await json_circuit.expectPass({ data: input, key1: key[0], key2: key[1], key3: key[2], key4: key[3], key5: key[4], key7: key[6], key8: key[7], key9: key[8] }, { value: output }); | ||
|
||
const lockData = readLockFile<Response>(`${httpLockfile}.json`); | ||
console.log("lockData: ", JSON.stringify(lockData)); | ||
|
||
const http = readHTTPInputFile(`${httpInputFile}`); | ||
const inputHttp = http.input; | ||
|
||
const headers = getHttpHeaders(lockData); | ||
|
||
const params = [inputHttp.length, http.bodyBytes.length, lockData.version.length, lockData.status.length, lockData.message.length]; | ||
headers.forEach(header => { | ||
params.push(header[0].length); | ||
params.push(header[1].length); | ||
}); | ||
|
||
// http_circuit = await circomkit.WitnessTester(`Extract`, { | ||
// file: `main/http_${httpCircuitName}`, | ||
// template: "LockHTTPResponse", | ||
// params: params, | ||
// }); | ||
// console.log("#constraints:", await http_circuit.getConstraintCount()); | ||
|
||
// match circuit output to original JSON value | ||
const circuitInput: any = { | ||
data: inputHttp, | ||
version: toByte(lockData.version), | ||
status: toByte(lockData.status), | ||
message: toByte(lockData.message), | ||
}; | ||
|
||
headers.forEach((header, index) => { | ||
circuitInput[`header${index + 1}`] = toByte(header[0]); | ||
circuitInput[`value${index + 1}`] = toByte(header[1]); | ||
}); | ||
|
||
// await http_circuit.expectPass(circuitInput, { body: http.bodyBytes }); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"version": "HTTP/1.1", | ||
"status": "200", | ||
"message": "OK", | ||
"headerName1": "content-type", | ||
"headerValue1": "application/json; charset=utf-8", | ||
"headerName2": "content-encoding", | ||
"headerValue2": "gzip" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
GET /v1/me/top/artists?time_range=medium_term&limit=1 HTTP/1.1 | ||
Host: api.spotify.com | ||
Authorization: Bearer BQBXRpIm2NL08akEiaxB5l42eiq6Zd9Q0S2-0Q4k0CMoa5u8o_ah_Ddjxt6Mv3226AEDyKYcFPpgw_6Asg-Y2hJpcuMya8wzqyqgiV-KH0vcEq7EFzODXoaBxsB0wryVCWDF6p5dqcpIHOz4QJqQa9mUA6sFzYNyECglT-BGcRe_N9f_3aqYTGQ-kkE-devPkPkEfDcbziT6mOzJfGRzLw | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to commit this token...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It expires every hour, at this point even I can't use it lol. but, yeah, will add this to gh secrets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
HTTP/1.1 200 OK | ||
content-type: application/json; charset=utf-8 | ||
content-encoding: gzip | ||
Transfer-Encoding: chunked | ||
|
||
{"data": {"me": {"profile": {"topArtists": {"__typename": "ArtistPageV2","items": [{"data": {"__typename": "Artist","profile": {"name": "Taylor Swift"},"uri": "spotify:artist:06HL4z0CvFAxyc27GXpf02" | ||
} | ||
} | ||
], | ||
"totalCount": 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"keys": [ | ||
"data", | ||
"me", | ||
"profile", | ||
"topArtists", | ||
"items", | ||
0, | ||
"data", | ||
"profile", | ||
"name" | ||
], | ||
"value_type": "string" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{"data": {"me": {"profile": {"topArtists": {"__typename": "ArtistPageV2","items": [{"data": {"__typename": "Artist","profile": {"name": "Taylor Swift"},"uri": "spotify:artist:06HL4z0CvFAxyc27GXpf02" | ||
} | ||
} | ||
], | ||
"totalCount": 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting. If the content is gzip vs identity, this likely fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use
Accept-Encoding: identity
in request and performLockHeader
proof on request, right?https://httpwg.org/specs/rfc9110.html#field.accept-encoding