Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

Commit c5524c1

Browse files
arangamaniahmdrz
authored andcommitted
Add some minor improvements. (#271)
* Added an ImportConfig function which will accept a parsed configuration file as a struct. * Added a SetHTTPTransport function which will enable setting a HTTP transport for tweaking low-level HTTP functionality. * Added a SetCookieJar function which enables using a custom cookie jar implementation.
1 parent fbf6e25 commit c5524c1

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

goinsta.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ func (inst *Instagram) SetHTTPClient(client *http.Client) {
7777
inst.c = client
7878
}
7979

80+
// SetHTTPTransport sets http transport. This further allows users to tweak the underlying
81+
// low level transport for adding additional fucntionalities.
82+
func (inst *Instagram) SetHTTPTransport(transport http.RoundTripper) {
83+
inst.c.Transport = transport
84+
}
85+
8086
// SetDeviceID sets device id
8187
func (inst *Instagram) SetDeviceID(id string) {
8288
inst.dID = id
@@ -92,6 +98,20 @@ func (inst *Instagram) SetPhoneID(id string) {
9298
inst.pid = id
9399
}
94100

101+
// SetCookieJar sets the Cookie Jar. This further allows to use a custom implementation
102+
// of a cookie jar which may be backed by a different data store such as redis.
103+
func (inst *Instagram) SetCookieJar(jar http.CookieJar) error {
104+
url, err := neturl.Parse(goInstaAPIUrl)
105+
if err != nil {
106+
return err
107+
}
108+
// First grab the cookies from the existing jar and we'll put it in the new jar.
109+
cookies := inst.c.Jar.Cookies(url)
110+
inst.c.Jar = jar
111+
inst.c.Jar.SetCookies(url, cookies)
112+
return nil
113+
}
114+
95115
// New creates Instagram structure
96116
func New(username, password string) *Instagram {
97117
// this call never returns error
@@ -209,21 +229,28 @@ func Export(inst *Instagram, writer io.Writer) error {
209229
//
210230
// This function does not set proxy automatically. Use SetProxy after this call.
211231
func ImportReader(r io.Reader) (*Instagram, error) {
212-
url, err := neturl.Parse(goInstaAPIUrl)
232+
bytes, err := ioutil.ReadAll(r)
213233
if err != nil {
214234
return nil, err
215235
}
216236

217-
bytes, err := ioutil.ReadAll(r)
237+
config := ConfigFile{}
238+
err = json.Unmarshal(bytes, &config)
218239
if err != nil {
219240
return nil, err
220241
}
242+
return ImportConfig(config)
243+
}
221244

222-
config := ConfigFile{}
223-
err = json.Unmarshal(bytes, &config)
245+
// ImportConfig imports instagram configuration from a configuration object.
246+
//
247+
// This function does not set proxy automatically. Use SetProxy after this call.
248+
func ImportConfig(config ConfigFile) (*Instagram, error) {
249+
url, err := neturl.Parse(goInstaAPIUrl)
224250
if err != nil {
225251
return nil, err
226252
}
253+
227254
inst := &Instagram{
228255
user: config.User,
229256
dID: config.DeviceID,

0 commit comments

Comments
 (0)