Collapse Drawer
Expand Drawer

C#

After downloading MultipartParser.cs and MultipartImage.cs and adding them to your project, you can use the parser like this:

    public static void FetchImages(RestClient client, String key)
    {
        var request = new RestRequest($"Property('{key}')/Media/All")
        {
            ResponseWriter = HandleMultipartStream
        };
        var response = client.DownloadData(request);
    }

    public static void HandleMultipartStream(Stream s)
    {
        MultipartParser parser = new MultipartParser(s);

        foreach (var image in parser.GetImages())
        {
            var filename = image.Headers["Content-ID"] + "_" + image.Headers["Object-ID"] + ".jpg";
            var f = File.OpenWrite(filename);
            image.Image.WriteTo(f);
            f.Close();
        }
    }

Go

After downloading MultipartParser.go and adding it to your project, you can use it like this:

func FetchImages(c *http.Client, key string) {
    resp, _ := c.Get(fmt.Sprintf("https://api-trestle.corelogic.com/trestle/odata/Property('%s')/Media/All", key))
    body := resp.Body
    defer body.Close()
    _ = MultipartParser(bufio.NewReader(body), WriteMultipartImage)
}

func WriteMultipartImage(h map[string]string, image []byte) {
        _ = ioutil.WriteFile(h["Content-ID"]+"_"+h["Object-ID"]+".jpg", image, 0666)
}

Java

After downloading MultipartParserIterator.java and MultipartImage.java and adding them to your project, you can use the parser like this:

    URI uri = oDataClient.newURIBuilder(service_uri)
            .appendEntitySetSegment("Property")
            .appendKeySegment("1234567")
            .appendNavigationSegment("Media/All")
            .build();
    ODataEntitySetIteratorRequest<ClientEntitySet, ClientEntity> request =
        oDataClient.getRetrieveRequestFactory().getEntitySetIteratorRequest(uri);
    request.setAccept("*/*");
    InputStream buf = request.rawExecute();
    MultipartParserIterator parserIterator = new MultipartParserIterator(buf);
    while (parserIterator.hasNext()) {
        MultipartImage image = parserIterator.next();
        HashMap<String, String> headers = image.getHeaders();
        String filename = headers.get("Content-ID") + "_" + headers.get("Object-ID") + ".jpg";
        FileOutputStream out = new FileOutputStream(filename);
        out.write(image.getImage());
        out.close();
    }

Node.js

You will need to install two packages as follows:

npm install formidable@canary node-fetch

Then after downloading multipartparser.js and adding it to your project, you can use the parser like this:

    function fileWriter(headers, image) {
        let fs = require('fs');
        let f = fs.createWriteStream(headers['Content-ID'] + '_' + headers['Object-ID'] + '.jpg');
        f.write(image);
        f.close();
    }

    async function FetchImages(key, accessToken) {
        const fetch = require("cross-fetch");
        let res = await fetch("https://api-trestle.corelogic.com/trestle/odata/Property('" + key + "')/Media/All",
            {headers: {Authorization: "Bearer " + accessToken.token.access_token}});
        let boundary = res.headers.get('content-type').split(';')[1].split('=')[1];
        let buffer = await res.buffer();
        const {MultipartParser} = require('./multipartparser');
        let parser = new MultipartParser(fileWriter);
        parser.parse(boundary, buffer);
    }

Python

After downloading multipartparser.py and adding it to your project, you can use it like this:

    def FetchImages(session, key):
        r = session.get(f"https://api-trestle.corelogic.com/trestle/odata/Property('{key}')/Media/All")
        parser = MultipartParser(BytesIO(r.content))
        for h, i in parser:
            f = open(f"{h['Content-ID']}_{h['Object-ID']}.jpg", "wb")
            f.write(i)
            print(f"Created {h['Content-ID']}_{h['Object-ID']}.jpg")