go-universal-network-adapter

module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2021 License: MIT

README

go-universal-network-adapter

GitHub license GitHub release

go-universal-network-adapter is a Go library for managing file downloads using different network protocols. It does a good job helping you build applications, in which remote files are being browsed, downloaded and processed. Universal Network Adapter offers an elegant abstraction over major network protocols and as such, it could be easily extended or just plugged into your project.

Install

go get -u github.com/goodsru/go-universal-network-adapter

Features

  • compatible with http/https/ftp/ftps/sftp - most popular protocols to work with file servers
  • tls support - allows connect to server with certificate (not only basic login/password)

Examples

See examples for a variety of examples.

Http:

package main

import (
	"fmt"
	"github.com/goodsru/go-universal-network-adapter/models"
	"github.com/goodsru/go-universal-network-adapter/services"
	"io/ioutil"
	"time"
)

func main() {
    adapter := services.NewUniversalNetworkAdapter()
    remoteFile, err := models.NewRemoteFile(models.NewDestination("http://lorempixel.com/400/200/", nil, nil))
    if err != nil {
        panic(err)
    }
    content, err := adapter.Download(remoteFile)
    if err != nil {
        panic(err)
    }
    
    fmt.Println(content.Name)
    fmt.Println(content.Path)
    buf, err := ioutil.ReadAll(content.Blob)
    fmt.Println(len(buf))
}

Ftp:

In case you don't have production ftp server, then for testing ftp downloading first you need to up ftp server on localhost, as it's implemented in this example.

package main

import (
	"fmt"
	"github.com/goodsru/go-universal-network-adapter/models"
	"github.com/goodsru/go-universal-network-adapter/services"
	"io/ioutil"
)

func main() {
	adapter := services.NewUniversalNetworkAdapter()

	remoteFile, err := models.NewRemoteFile(models.NewDestination("ftp://localhost:21/test1.txt", &models.Credentials{
		User:     "user",
		Password: "pass",
	}, nil))

	if err != nil {
		panic(err)
	}
	content, err := adapter.Download(remoteFile)
	if err != nil {
		panic(err)
	}

	fmt.Println(content.Name)
	fmt.Println(content.Path)
	buf, err := ioutil.ReadAll(content.Blob)
	fmt.Println(string(buf))
}

Sftp:

In case you don't have some production sftp server, then for testing sftp downloading first you need to up sftp server on localhost, as it's implemented in this example.

package main

import (
	"fmt"
	"io/ioutil"
	"github.com/goodsru/go-universal-network-adapter/models"
	"github.com/goodsru/go-universal-network-adapter/services"
)

func main() {
	adapter := services.NewUniversalNetworkAdapter()

	remoteFile, err := models.NewRemoteFile(models.NewDestination("sftp://host.com:22/folder/file.json", &models.Credentials{
		User:     "Admin",
		Password: "Qwerty",
	}, nil))

	if err != nil {
		panic(err)
	}
	content, err := adapter.Download(remoteFile)
	if err != nil {
		panic(err)
	}

	fmt.Println(content.Name)
	fmt.Println(content.Path)
	buf, err := ioutil.ReadAll(content.Blob)
	fmt.Println(string(buf))
}

Credits

License

The MIT License (MIT)

Copyright (c) 2019 LLC Marketplace

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Directories

Path Synopsis
universal-network-adapter interfaces
universal-network-adapter interfaces
universal-network-adapter models
universal-network-adapter models
downloader/http
Package contains realization of class for file download via HTTP protocol
Package contains realization of class for file download via HTTP protocol
downloader/sftp
sftp file downloader implementation
sftp file downloader implementation
tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL